User Registration का Form
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.
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.
<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.
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. #}
- User द्वारा typed raw password save करना बजाय UserCreationForm उपयोग करने के, जो इसे अपने आप hash करता है।
- POST branch के अंदर
form.save()call करना भूल जाना, इसलिए account कभी बनता ही नहीं। - 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 कर दें।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Introduction to Django's Auth System
- The User Model Overview
- Setting Up a Login View
- Logout Functionality
- User Registration Form
- The login_required Decorator
- The permission_required Decorator
- Django Groups and Permissions
- Password Hashing in Django
- Session Authentication Basics
- Introduction to Custom User Models
- Sending Emails with Django