Friday, September 26, 2014

Re: at django login my request.POST.get data are all empty, I struggle with what I missed there

Thank you Alejandro, it must be the template somehow: I had a seperate login form: 

{% extends "base/base.html" %}
{% load crispy_forms_tags %}
{% block head_content %}
    <div id="heading" class="jumbotron">
<div class ="container">
<div class ="row">
        <h2>Anmeldung bei Netteachers</h2>
</div></div>
    </div>
{% endblock %}
{% block main_content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<div class ="container">
<div class ="row">
     <div class="col-md-6 col-lg-6 col-sm-6">
        <form id="login_form" method="post" action="{% url 'userauth:login' %}">
            {% form crispy %}
            {% csrf_token %}
           <button type="submit" class="btn btn-success">Anmelden</button>  
      </form>
     </div>
     <div class="col-md-6 col-lg-6 col-sm-6">
         <h2>Eine Baustelle</h2>
         Derzeit werden logins nur für Administratoren verteilt. Netteachers ist noch in der Aufbauphase.
     </div>
</div>
</div>
{% endblock %}

This did not work. Forms.py is this:
from django import forms

class LoginForm(forms.Form):

    username = forms.CharField(
        max_length=100,
        widget=forms.TextInput,
        required=False,
        label="",
    )
    password = forms.CharField(
        max_length=100,
        widget=forms.TextInput,
        required=False,
        label="",
    )


I then put the form back in the navigation bar, where it did work. I will check out your links. I am still a little confused by the formhandling overall.
    Thanks a lot für your very valuable input.
           Sabine




Am Samstag, 27. September 2014 00:08:08 UTC+2 schrieb Alejandro Varas:
Hi Sabine,

Your code looks good. Can you post you template code?

Why are you not using form `is_valid`? [0]

What version of Django are you using? You can be facing that your
request.POST is empty! [1]

This may be not what you are looking for but you can use Django
Authentication System [2]

Best

0 - https://docs.djangoproject.com/en/1.6/ref/forms/api/#using-forms-to-validate-data
1 - https://docs.djangoproject.com/en/1.6/ref/request-response/#django.http.HttpRequest.POST
2 - https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.views.login

On Fri, Sep 26, 2014 at 6:32 PM, Sabine Maennel
<sabine....@gmail.com> wrote:
> This is my login view:
>
> def login_view(request,template_name='userauth/login.html'):
>
>     if request.method == 'POST':
>         username = request.POST.get('username')
>         password = request.POST.get('password')
>         print username
>         print password
>         user = authenticate(username=username, password=password)
>         if user is not None:
>             if user.is_active:
>                 login(request, user)
>                 next_page = reverse('public:courselist')
>             else:
>                 next_page = reverse('userauth:login')
>                 messages.error(request, "Ihr Account wurde deaktiviert.
> Bitte nehmen Sie Kontakt zu uns auf, wenn Sie Ihnen wieder aktivieren
> wollen.")
>         else:
>             next_page = reverse('userauth:login')
>             messages.error(request, u'Ung\u00FCltiges Login')
>     else:
>         dictionary = {'form': form }
>         return render_to_response('userauth/login.html', dictionary,
> context_instance=RequestContext(request))
>
>     return HttpResponseRedirect(next_page)
>
>
> And this is my form:
> from django import forms
>
> class LoginForm(forms.Form):
>
>     username = forms.CharField(
>         max_length=100,
>         widget=forms.TextInput,
>         required=False,
>         label="",
>     )
>     password = forms.CharField(
>         max_length=100,
>         widget=forms.TextInput,
>         required=False,
>         label="",
>     )
>
> I think I need to tell Django somehow about the form, but I do need really
> know where? Can someone please help me with figuring this out? When I print
> out the username and password they are both None.
>
>   Thanks in advance
>         Sabine
>
>
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/db52e99e-0ce1-469c-91f7-80fdad3272eb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Alejandro Varas G.
http://alejandrovaras.me

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1bc80193-7384-41e7-9857-974d95a48cb4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment