0
votes

Actually when I register with a user name it redirects to registration-completion page.But when I login with that user name it throws form error. So when I login with my created user name it shows

"Your username and password didn't match. Please try again"

(which I wrote to show form.errors in login.html )

login.html

{% extends "base.html" %}

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>

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

{% endblock %}

registration.html

<html>
<head>
  <title>Register</title>
</head>   

<body>
  <h2>Register</h2>
  <form action="/accounts/register/" method="POST">{% csrf_token %}
     <table>
        {{form.as_table}}
     </table>

  <p>
     <input type="submit" value="Submit">
  </p>
  </form>   
</body>
</html>

urls.py

enter code here
from django.conf.urls import patterns, include, url
from frm.views import main,forum,thread
from frm.views import reply,new_thread,profile,post
from django.conf import settings
from django.contrib import admin
from django.conf.urls import *
from django.contrib.auth.views import login
admin.autodiscover()

urlpatterns = patterns('',
url(r'^$',main),
url(r'^forum/(?P<pk>\w+)/$',forum  ),
url(r'^thread/(?P<pk>\w+)/$',thread ),
url(r'^post/(new_thread|reply)/(\d+)',post),
url(r'^new_thread/(\d+)/$',new_thread),
url(r'^reply/(\d+)/$',reply),
url(r'^profile/(?P<pk>\w+)/$',profile),

# Examples:
# url(r'^$', 'forum.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^accounts/',include('registration.backends.default.urls')),

url(r'^admin/', include(admin.site.urls)),
)
1
Hi welcome to SO. Please detail each of the steps you are taking during the registration process, then post the exact error you are getting. Try and identify where the bug is in your code from the error you see and post that code as well. - Deepend
thanks. I have updated the question . - Lakshya Garg

1 Answers

0
votes

You can access the errors in the template in many ways. Try changing:

{% if form.errors %}
  <p>Your username and password didn't match. Please try again.</p>
{% endif %}

to:

{% if form.errors %}
  {{ form.errors }}
  <br /><br />

  {% if form.non_field_errors %}
    {% for error in form.non_field_errors %}
      {{ error }}
    {% endfor %}

    <br /><br />
    a nicer way:<br />
    {{ form.non_field_errors.as_ul }}

  {% endif %}

  <br /><br />
  form username errors:<br />
  {{ form.username.errors }}

  <br /><br />
  form password errors:<br />
  {{ form.password.errors }}
{% endif %}

That is a good start. You will also want to show the errors to your visitors. Change your label_tags to

<td>{{form.username.errors }}<br />{{ form.username.label_tag }}</td>

and

<td>{{form.password.errors }}<br />{{ form.password.label_tag }}</td>