开发者

MultiValueKeyDict error in Django authentication documentation

开发者 https://www.devze.com 2022-12-18 16:42 出处:网络
I\'m trying to follow the code from http://docs.djangoproject.com/en/1.1/topics/auth/ regarding user logins.

I'm trying to follow the code from http://docs.djangoproject.com/en/1.1/topics/auth/ regarding user logins.

def my_view(request):
     username = request.POST['username']
     password = request.POST['password']
     user = authenticate(username=username, password=password开发者_如何学C)
     if user is not None:
         if user.is_active:
             login(request, user)
             HttpResponseRedirect('/%s/'%username) 
         else:
             #Logic will go here later
             pass
     else:
         # Return an 'invalid login' error message.
         pass

My problem is that when I attempt to use this new view, I get the following error message: ('expected an indented block', ('C:\emodel_tracking\..\emodel_tracking\tracker\views.py', 50, 4, ' else:\n'))

I can't see how the formatting on the else statement could be wrong; I'm using the code straight from the django documentation. Any ideas what's wrong with the "else" statement?

edit: OK, using pass fixed that error - now I'm getting guff about: MultiValueDictKeyError at /login/ --- Key 'username' not found in QueryDict: {}. Thoughts? I'm not quite sure how to proceed - my guess is that I need to somehow tie this to an html form similar to this one:

  {% extends "base.html" %}

  {% block content %}

  {% if form.errors %}
    <p class="error">Sorry, that's not a valid username or password</p>
  {% endif %}

  <form action="" method="post">
    <label for="username">User name:</label>
    <input type="text" name="username" value="" id="username">
    <label for="password">Password:</label>
    <input type="password" name="password" value="" id="password">

    <input type="submit" value="login" />
    <input type="hidden" name="next" value="{{ next|escape }}" />
  </form>

{% endblock %}

If this is the case, how do I render out the form (which if I'm not mistaken, will require a 'return' statement) prior to me requesting the information that form requires?


To answer your second question:
When the view is executed, it tries to read a username from POST data.

That means, your view should only be accessed after submitting the form (i.e. via POST together with username and password data) or you have to change your view:

def my_view(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        # gon on ...
    else:
        # just show the form

Btw it is better to follow this tutorial than copy and pasting code fragments from a documentation page and try to get it working. These are just fragments, they don't contain all the needed code.
No wonder that the code is not working.

Read the Django book and then you can ask question if something is not working.


there's nothing in the "else"; If you don't want to do a thing there say pass. The django documentation expect you to implement your own code there so if you don't put anything there it detects it as a indentation error. If you don't have any code to implement you can hint python to just ignore the else.

else:
    pass


instead of pass as suggested, it's probably a good idea to actually put something.

i use simple errors in debugging, but you may want to redirect to some kind of error page. either way, here's a minimalistic method to alert the user.

return HttpResponse('ERROR: Account Disabled.')
0

精彩评论

暂无评论...
验证码 换一张
取 消