How to make Django generic views accessible to logged in users only?
<p>I've implemented the social-auth for Google for users to log in. I cannot understand how to restrict the visitors from accessing the generic views like UpdateView, ListView or CreateView, if they are not logged in with the social-auth system. Here is the code.</p>

<p><strong><em>views.py</em></strong></p>

<pre><code>class AchievementCreate(CreateView):
    form_class = AchievementForm2
    model = Achievements
    def form_valid(self, form):
            achieves = form.save(commit=False)
            achieves.resume = Resume.objects.get(pk=2)
            return super(AchievementCreate, self).form_valid(form)

def home(request):
#logout(request)
    uname=""
    if request.method == 'POST' and 'submit' in request.POST:
        submit = request.POST['submit']
        if submit=="sign-out":
            logout(request)

    if '_auth_user_id' in request.session:
        uname=sm.UserSocialAuth.objects.get(
        user_id=int(request.session['_auth_user_id'])
        ).user
        request.session['uname']=str(uname)
    return render(request,'cv/home.html',{'uname': uname})
</code></pre>

<p><strong><em>home.html</em></strong></p>

<pre><code> &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
        &lt;meta charset="utf-8"/&gt;
        &lt;title&gt;Home&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form method="post" action="{% url 'cv:home' %}"&gt;{% csrf_token %}
            {% if uname %}
                Helo, {{uname}}&lt;br&gt;
                The user already signed in. He may need to sign out&lt;br&gt;
                &lt;input type="submit" name="submit" value="sign-out"&gt;            
            {% else %}
                Please sign-in&lt;br&gt;
                &lt;a href="{% url 'social:begin' 'google-oauth2' %}"&gt;Google login&lt;/a&gt;
            {% endif %}  
        &lt;/form&gt; 
    &lt;/body&gt;
    &lt;/html&gt;
</code></pre>

<p><strong><em>urls.py</em></strong></p>

<pre><code>urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^achievements/(?P&lt;pk&gt;[0-9]+)/delete/$', views.AchievementDelete.as_view(), name='achievement-delete'),
url(r'^achievements/(?P&lt;pk&gt;[0-9]+)/$', views.AchievementUpdate.as_view(), name='achievement-update'),
url(r'^achievements/add/$', login_required(views.AchievementCreate.as_view()), name='achievement-add'),
url(r'^home/$', views.home, name='home'),]
</code></pre>

<p>I want the "AchievementCreate" class to be accessible only if the user has logged in. But I don't understand how. Is it possible to use sessions? How, in this case?</p>

<p>So you are using 'social-auth' for your login system. In your case, you will have to use a custom function. I'm assuming you can access the logged in user by referring to:
<code>request.SESSION</code></p>

<p>If so, then you can override the dispatch method of your CreateView:</p>

<pre><code>class AchievementCreate(CreateView):
   .....
   .....
   def dispatch(self, request, *args, **kwargs):
      user = request.session['_auth_user_id']
      # handle authentication
</code></pre>

python django django-socialauth