LoginRequiredMixin ignored

842 views
Skip to first unread message

Lee Hinde

unread,
Jun 15, 2016, 6:56:38 PM6/15/16
to django...@googlegroups.com
Using Django 1.9.7.
I have the following class:
class AddView(LoginRequiredMixin, CreateView):
template_name = "entry.html"
model = Property
form_class = AddPropertyForm
success_url = "/buy/"
login_url = '/account/login/'

def dispatch(self, request, *args, **kwargs):
print request.user
print request.user.is_authenticated()
return super(AddView, self).post(request, *args, **kwargs)

def form_valid(self, form):
ls_property = form.save(commit=False)
ls_property.user = get_user(self.request)
ls_property.latitude = form.cleaned_data['latitude']
ls_property.longitude = form.cleaned_data['longitude']
submit = form.cleaned_data['submit']
ls_property.visible = (submit != 'draft')
ls_property.save()
return super(AddView, self).form_valid(form)

The LoginRequiredMixin is ignored, i.e, I can get to the page without being logged in.
request.user reports: AnonymousUser
request.user.is_authenticated() reports: False
If I wrap the url:
url(r'^sell', user_passes_test(user_is_active)(AddView.as_view()), name="add"),
the page is protected, so I have a work around.
This seems basic and I don't find other reports of LoginRequiredMixin not working, so I assume it's me.
What am I missing?



Brian Schott

unread,
Jun 15, 2016, 7:27:47 PM6/15/16
to django...@googlegroups.com
Try swapping class order.

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BePoMzApQGdzZ9AW47CjF3yhT0Smnc1AZw%2BOQ8uTcH1G3%3Dasg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Babatunde Akinyanmi

unread,
Jun 16, 2016, 2:51:45 AM6/16/16
to Django users

Actually it's you. Please see inline:

On Jun 15, 2016 11:56 PM, "Lee Hinde" <leeh...@gmail.com> wrote:
>
> Using Django 1.9.7.
>
> I have the following class:
>
> class AddView(LoginRequiredMixin, CreateView):
>     template_name = "entry.html"
>     model = Property
>     form_class = AddPropertyForm
>     success_url = "/buy/"
>     login_url = '/account/login/'
>

Consider your code for `dispatch`


>     def dispatch(self, request, *args, **kwargs):
>         print request.user
>         print request.user.is_authenticated()
>         return super(AddView, self).post(request, *args, **kwargs)
>

You have bypassed `LoginRequiredMixin` implementation of `dispatch` and there's where the login logic is done. You should be returning a super call to `dispatch` not `post`. If you are actually trying to force all requests into POSTs (which I doubt), you are better off doing it in the `get` method.

>     def form_valid(self, form):
>         ls_property = form.save(commit=False)
>         ls_property.user = get_user(self.request)
>         ls_property.latitude = form.cleaned_data['latitude']
>         ls_property.longitude = form.cleaned_data['longitude']
>         submit =  form.cleaned_data['submit']
>         ls_property.visible = (submit != 'draft')
>         ls_property.save()
>         return super(AddView, self).form_valid(form)
>
>
> The LoginRequiredMixin is ignored, i.e, I can get to the page without being logged in.
>
> request.user reports: AnonymousUser
> request.user.is_authenticated() reports: False
>
> If I wrap the url:
>
> url(r'^sell', user_passes_test(user_is_active)(AddView.as_view()), name="add"),
>
> the page is protected, so I have a work around.
>
> This seems basic and I don't find other reports of LoginRequiredMixin not working, so I assume it's me.
>
> What am I missing?
>
>
>
>

Lee Hinde

unread,
Jun 16, 2016, 1:30:07 PM6/16/16
to django...@googlegroups.com
I always assume it's me. That was some errant copy and paste.

Thanks!

Sergiy Khohlov

unread,
Jun 16, 2016, 2:40:15 PM6/16/16
to django-users
please send  your http header. Look like auth header is present by  browser default 

Вірусів немає. www.avast.com

Many thanks,

Serge


+380 636150445
skype: skhohlov

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages