← Back to Django Course | Chapter 10: Authentication & Authorization | Lesson 5 of 12

User Registration का Form

एक registration form वह sign-up sheet है जहां एक नया visitor अपना नाम लिखता है और join करने के लिए एक secret password चुनता है।
Syntax
markup
from django.contrib.auth.forms import UserCreationForm

def register(request):
    form = UserCreationForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('login')
    return render(request, 'register.html', {'form': form})

UserCreationForm उपयोग करना

UserCreationForm username, password, और password confirmation fields handle करता है, साथ ही hashing भी, बिना आपको वह logic कुछ भी लिखे।

उदाहरण: Using UserCreationForm

form.save() creates the User with a properly hashed password; login() then signs them in right away.

markup
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.shortcuts import render, redirect

def register_view(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('home')
    else:
        form = UserCreationForm()
    return render(request, 'register.html', {'form': form})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Registration Form Render करना

Template form fields के through loop करता है ताकि हर एक को अपना label और input अपने आप मिल जाए।

उदाहरण: Rendering the Registration Form

form.as_p renders each field wrapped in a <p>, each with a matching <label> and name attribute already built in.

markup
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Register</button>
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Registration URL Wire करना

Register view को एक URL entry चाहिए ताकि users sign-up page तक पहुंच सकें।

उदाहरण: Wiring the Registration URL

This maps /register/ to register_view, matching the form's action target.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('register/', views.register_view, name='register'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. User द्वारा typed raw password save करना बजाय UserCreationForm उपयोग करने के, जो इसे अपने आप hash करता है।
  2. POST branch के अंदर form.save() call करना भूल जाना, इसलिए account कभी बनता ही नहीं।
  3. Registration के ठीक बाद user को log in न करना, उन्हें sign up करने के ठीक बाद फिर से log in करने पर मजबूर करते हुए।
चैप्टर सारांश
  • UserCreationForm एक hashed password के साथ नए users register करने के लिए Django का built-in form है।
  • यह save करने से पहले जांचता है कि दोनों password fields match करते हैं।
  • form.save() नया User instance बनाता है और return करता है।
  • एक smoother experience के लिए optionally registration के ठीक बाद नए user को log in कर दें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.