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?
--
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.
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?
>
>
>
>
--
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%2BePoMwMHRW6cEbzPypBhTnGCRJ7W0nGE%2BvaQ_ytmA_D2WhiSg%40mail.gmail.com.