aboutsummaryrefslogtreecommitdiff
path: root/files/tr/learn/server-side/django/sessions/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/tr/learn/server-side/django/sessions/index.html')
-rw-r--r--files/tr/learn/server-side/django/sessions/index.html182
1 files changed, 182 insertions, 0 deletions
diff --git a/files/tr/learn/server-side/django/sessions/index.html b/files/tr/learn/server-side/django/sessions/index.html
new file mode 100644
index 0000000000..594cab9f47
--- /dev/null
+++ b/files/tr/learn/server-side/django/sessions/index.html
@@ -0,0 +1,182 @@
+---
+title: 'Django Tutorial Part 7: Sessions framework'
+slug: Öğren/Server-side/Django/Sessions
+tags:
+ - Başlangıç
+ - CondingScripting
+ - Makale
+ - Python
+ - Server
+ - Sunucu
+ - Tutorial
+ - django
+ - django oturumları
+ - django sessions
+ - oturumlar
+ - server-side
+ - sessions
+ - sunucu tarafı
+ - Öğretici
+ - öğren
+translation_of: Learn/Server-side/Django/Sessions
+---
+<div>{{LearnSidebar}}</div>
+
+<div>{{PreviousMenuNext("Learn/Server-side/Django/Generic_views", "Learn/Server-side/Django/authentication_and_sessions", "Learn/Server-side/Django")}}</div>
+
+<p class="summary">Bu tutorial, anasayfaya session temelli bir ziyareti sayacı ekleyerek <a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Tutorial_local_library_website">LocalLibrary</a> websitemizi genişletmektedir. Bu görece basit bir örnek, fakat kendi sitelerinizdeki bilinmeyen kullanıcılar için kalıcı davranış sağlamak için session framework'ünü nasıl kullanacağınızı göstermektedir.</p>
+
+<table class="learn-box standard-table">
+ <tbody>
+ <tr>
+ <th scope="row">Ön şartlar:</th>
+ <td><a href="/en-US/docs/Learn/Server-side/Django/Generic_views">Django Tutorial 6. Bölüm: Generic list ve detail view'lar</a> dahil önceki tüm tutorial konularını tamamlayın</td>
+ </tr>
+ <tr>
+ <th scope="row">Amaç:</th>
+ <td>Session'ların nasıl kullanıldığını anlamak.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Genel_Bakış">Genel Bakış</h2>
+
+<p>Önceki tutorial'larda oluşturduğumuz <a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Tutorial_local_library_website">LocalLibrary</a> websitesi, kullanıcıların katalogdaki kitap ve yazarlara göz atmalarını sağlar. İçerik, veri tabanından dinamik olarak üretilirken, tüm kullanıcılar siteyi kullandıklarında esasen aynı sayfalar ve bilgi türlerine erişebilecektir.</p>
+
+<p>"Gerçek" bir kütüphanede, her bir kullanıcıya önceki kullanım ve tercihlerini vs. temel alarak kişiselleştirilmiş bir deneyim sunmayı isteyebilirsiniz. Örneğin, kullanıcının daha önce kabul ettiği uyarı iletilerini siteyi veya mağazayı bir sonraki ziyaret edişlerinde gizleyebilir ve tercihlerine (örn. her bir sayfada görüntülenmesini istedikleri arama sonucu sayısı gibi) saygı duyabilirsiniz. </p>
+
+<p>Session framework'ü her bir site ziyaretçisine dayalı keyfi veriler depolamanızı ve geri almanızı sağlayarak, bu tür davranışları uygulamanızı sağlar. </p>
+
+<h2 id="Sessions_Nedir">Sessions Nedir?</h2>
+
+<p>Web tarayıcılar ve sunucular arasındaki tüm iletişim, <em>stateless</em> bir HTTP protokolü aracılığıyladır.  Protokolün stateless oluşundan kasıt, client ve server arasındaki iletilerin birbirlerinden tamamen bağımsız olduğudur— önceki iletilere dayalı davranış veya "dizi" kavramı bulunmamaktadır. Sonuç olarak, <span class="translation">bir istemci ile devam eden ilişkileri takip eden bir siteye sahip olmak istiyorsanız</span>, bunu kendiniz uygulamanız gerekiyor.</p>
+
+<p>Sessions are the mechanism used by Django (and most of the Internet) for keeping track of the "state" between the site and a particular browser. Session'lar her bir tarayıcı için keyfi veriler depolamanızı sağlar ve <span class="translation">tarayıcı bağlandığında bu verileri sitede bulabilirsiniz</span>. Session ile ilişkili her bir veri öğesi  Individual data items associated with the session are then referenced by a "key", which is used both to store and retrieve the data.</p>
+
+<p>Django uses a cookie containing a special <em>session id</em> to identify each browser and its associated session with the site. The actual session <em>data</em> is stored in the site database by default (this is more secure than storing the data in a cookie, where they are more vulnerable to malicious users). You can configure Django to store the session data in other places (cache, files, "secure" cookies), but the default location is a good and relatively secure option.</p>
+
+<h2 id="Session'ları_Etkinleştirme">Session'ları Etkinleştirme</h2>
+
+<p>Sessions <a href="/en-US/docs/Learn/Server-side/Django/skeleton_website">iskelet websitesini oluşturduğumuzda</a> (tutorial 2'de) otomatik olarak etkinleştirilir.</p>
+
+<p>Aşağıda görüldüğü gibi, yapılandırma, proje dosyasının (<strong>locallibrary/locallibrary/settings.py</strong>) <code>INSTALLED_APPS</code> ve <code>MIDDLEWARE</code> kısımlarına kurulur:</p>
+
+<pre class="brush: python">INSTALLED_APPS = [
+ ...
+<strong> 'django.contrib.sessions',</strong>
+ ....
+
+MIDDLEWARE = [
+ ...
+<strong> 'django.contrib.sessions.middleware.SessionMiddleware',</strong>
+ ....</pre>
+
+<h2 id="Session'ları_Kullanma">Session'ları Kullanma</h2>
+
+<p>You can access the <code>session</code> attribute in the view from the <code>request</code> parametresi (an <code>HttpRequest</code> passed in as the first argument to the view). This session attribute represents the specific connection to the current user (or to be more precise, the connection to the current <em>browser</em>, as identified by the session id in the browser's cookie for this site).</p>
+
+<p><code>session</code> attribute'u view'ınızda istediğiniz kadar yazıp okuyabileceğiniz sözlük benzeri bir nesne olup, dilediğiniz gibi değiştirilebilirdir. You can do all the normal dictionary operations, including clearing all data, testing if a key is present, looping through data, etc. Most of the time though, you'll just use the standard "dictionary" API to get and set values.</p>
+
+<p>The code fragments below show how you can get, set, and delete some data with the key "<code>my_car</code>", associated with the current session (browser). </p>
+
+<div class="note">
+<p><strong>Note</strong>: One of the great things about Django is that you don't need to think about the mechanisms that tie the session to your current request in your view. If we were to use the fragments below in our view, we'd know that the information about <code>my_car</code> is associated only with the browser that sent the current request.</p>
+</div>
+
+<pre class="brush: python"># Get a session value by its key (e.g. 'my_car'), raising a KeyError if the key is not present
+my_car = request.session['my_car']
+
+# Get a session value, setting a default if it is not present ('mini')
+my_car = request.session.get('my_car', 'mini')
+
+# Set a session value
+request.session['my_car'] = 'mini'
+
+# Delete a session value
+del request.session['my_car']
+</pre>
+
+<p>The API also offers a number of other methods that are mostly used to manage the associated session cookie.  For example, there are methods to test that cookies are supported in the client browser, to set and check cookie expiry dates, and to clear expired sessions from the data store. You can find out about the full API in <a href="https://docs.djangoproject.com/en/1.10/topics/http/sessions/">How to use sessions</a> (Django belgeleri).</p>
+
+<h2 id="Session_Verisini_Kaydetmek">Session Verisini Kaydetmek</h2>
+
+<p>Varsayılan olarak, Django yalnızca session veri tabanını kaydeder ve session <em>değiştirildiğinde</em> veya <em>silindiğinde</em> istemciye session çerezini gönderir. Önceki kısımlarda görüldüğü gibi session anahtarını kullanarak bazı verileri güncellerseniz, sonrasında bu konuda endişelenmenize gerek kalmaz! Örneğin:</p>
+
+<pre class="brush: python"># This is detected as an update to the session, so session data is saved.
+request.session['my_car'] = 'mini'</pre>
+
+<p>Session verisi <em>içersinde</em> bazı bilgileri güncellerseniz, sonrasında Django session'a bir değişiklik yaptığınızı ve veriyi kaydettiğinizi fark etmeyecektir (örneğin, aşağıda gösterildiği gibi, "<code>my_car</code>" verinizin  içerisindeki "<code>wheels</code>" verinizi değiştirmek üzereyseniz). Bu durumda session'ı açık bir şekilde değiştirilmiş olarak işaretlemeniz gerekecektir.</p>
+
+<pre class="brush: python"># Session object not directly modified, only data within the session. Session changes not saved!
+request.session['my_car']['wheels'] = 'alloy'
+
+# Set session as modified to force data updates/cookie to be saved.
+<code>request.session.modified = True</code>
+</pre>
+
+<div class="note">
+<p><strong>Not</strong>: Davranışı değiştirebilirsiniz; böylece site veri tabanını güncelleyecek/proje ayarlarınıza (<strong>locallibrary/locallibrary/settings.py</strong>) <code>SESSION_SAVE_EVERY_REQUEST = True</code> komutunu ekleyerek her bir request'teki çerezi gönderecektir.</p>
+</div>
+
+<h2 id="Basit_Örnek_—_Ziyaret_Sayısını_Almak">Basit Örnek — Ziyaret Sayısını Almak</h2>
+
+<p>Basit bir gerçek dünya örneği olarak, mevcut kullanıcıya <em>LocalLibrary</em> anasayfasını kaç kere ziyaret ettiklerini söyleyemesi için kütüphanemizi güncelleyeceğiz.</p>
+
+<p><strong>/locallibrary/catalog/views.py</strong> dosyasını açın ve aşağıda kalın olarak gösterilen değişiklikleri yapın. </p>
+
+<pre class="brush: python">def index(request):
+ ...
+
+ num_authors=Author.objects.count() # The 'all()' is implied by default.
+
+<strong> # Number of visits to this view, as counted in the session variable.
+ num_visits=request.session.get('num_visits', 0)
+ request.session['num_visits'] = num_visits+1</strong>
+
+ # Render the HTML template index.html with the data in the context variable.
+ return render(
+ request,
+ 'index.html',
+<strong> context={'num_books':num_books,'num_instances':num_instances,'num_instances_available':num_instances_available,'num_authors':num_authors,
+ 'num_visits':num_visits}, # num_visits appended</strong>
+ )</pre>
+
+<p>Here we first get the value of the <code>'num_visits'</code> session key, setting the value to 0 if it has not previously been set. Each time a request is received, we then increment the value and store it back in the session (for the next time the user visits the page). The <code>num_visits</code> variable is then passed to the template in our context variable.  </p>
+
+<div class="note">
+<p><strong>Note</strong>: We might also test whether cookies are even supported in the browser here (see <a href="https://docs.djangoproject.com/en/1.10/topics/http/sessions/">How to use sessions</a> for examples) or design our UI so that it doesn't matter whether or not cookies are supported.</p>
+</div>
+
+<p>Add the line seen at the bottom of the following block to your main HTML template (<strong>/locallibrary/catalog/templates/index.html</strong>) at the bottom of the "Dynamic content" section to display the context variable:</p>
+
+<pre class="brush: html">&lt;h2&gt;Dynamic content&lt;/h2&gt;
+
+&lt;p&gt;The library has the following record counts:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;strong&gt;Books:&lt;/strong&gt; \{{ num_books }}&lt;/li&gt;
+&lt;li&gt;&lt;strong&gt;Copies:&lt;/strong&gt; \{{ num_instances }}&lt;/li&gt;
+&lt;li&gt;&lt;strong&gt;Copies available:&lt;/strong&gt; \{{ num_instances_available }}&lt;/li&gt;
+&lt;li&gt;&lt;strong&gt;Authors:&lt;/strong&gt; \{{ num_authors }}&lt;/li&gt;
+&lt;/ul&gt;
+
+<strong>&lt;p&gt;You have visited this page \{{ num_visits }}{% if num_visits == 1 %} time{% else %} times{% endif %}.&lt;/p&gt;</strong>
+</pre>
+
+<p>Değişikliklerinizi kaydedin ve test sunucusunu yeniden başlatın. Sayfayı her yenilediğinizde, numara güncellenmelidir.</p>
+
+<ul>
+</ul>
+
+<h2 id="Özet">Özet</h2>
+
+<p><em>Bilinmeyen</em> kullanıcılarla etkileşiminizi arttırmak için oturumları kullanmanın ne kadar kolay olduğunu artık biliyorsunuz.</p>
+
+<p>Sıradaki makalelerimizde, authentication ve authorisation (izin) framework'ünü açıklayacağız ve kullanıcı hesaplarını nasıl destekleyeceğinizi göstereceğiz.</p>
+
+<h2 id="Ayrıca_bakın">Ayrıca bakın</h2>
+
+<ul>
+ <li><a href="https://docs.djangoproject.com/en/1.10/topics/http/sessions/">Session'lar nasıl kullanılır</a> (Django belgeleri)</li>
+</ul>
+
+<p>{{PreviousMenuNext("Learn/Server-side/Django/Generic_views", "Learn/Server-side/Django/Authentication", "Learn/Server-side/Django")}}</p>