سؤال

I followed this guide to extend registration in Django and it all seemed working until I clicked register and nothing happens. I checked in the command line with sql and table exists as per specification, however when I try to view items with UserProfile.objects.all() it returns empty list. Seems like nothing is getting sent anywhere upon form submission.

I get no errors, so bit confused on what is the issue.

models.py

def user_registered_callback(sender, user, request, **kwargs):
    profile = UserProfile(user = user)
    profile.first_name = str(request.POST["first_name"])
    profile.last_name = str(request.POST["last_name"])
    profile.city = str(request.POST["city"])
    profile.country = str(request.POST["country"])
    profile.save()

user_registered.connect(user_registered_callback)

forms.py

from registration.forms import RegistrationForm

class CustomRegistrationForms(RegistrationForm):
    first_name = forms.CharField(label ="First Name")
    last_name = forms.CharField(label ="Last Name")
    city = forms.CharField(label ="City")
    country = forms.CharField(label ="Country")

urls.py

url(r'^accounts/register/$', RegistrationView.as_view(form_class = CustomRegistrationForms), 
    name = 'registration_register', kwargs=dict(extra_context={'next_page': '/services/'})),
url(r'^accounts/', include('registration.backends.simple.urls'))
  ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

registration_form.html

<form method="post" action="" class="wide">
{% csrf_token %}

  ..sample form
  <label for="id_username">Username:</label>
  {% if form.username.errors %}
    <p class="errors">{{ form.username.errors.as_text }}</p>
  {% endif %}
  {{ form.username }}
  <input type="submit" class="btn btn-default btn-sm" value="Register"></input>                                           
</form>
هل كانت مفيدة؟

المحلول

Remove action from form tag completely. This will result in sending the form request as HTTP-POST to itself, in this case "/accounts/register". But this is not your main problem.

According to the documentation (https://django-registration.readthedocs.org/en/latest/forms.html) after reading I'm pretty sure you are missing the fields required by registration.forms.RegistrationForm which you are subclassing. The backend will take care of these and reject the given values. Because you are only showing username and its errors in your template the other validation errors will never show up.

Add the required fields to your form and try again. You may want to simply render it through

{{ form.as_p }}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top