ઉકેલાયેલ: Django સાઇનઅપ urls.py

Django Signup urls.py ની મુખ્ય સમસ્યા એ છે કે તે સારી રીતે ડિઝાઇન કરેલ નથી. કોડ અવ્યવસ્થિત અને વાંચવામાં મુશ્કેલ છે, અને તે શ્રેષ્ઠ પ્રથાઓનું પાલન કરતું નથી. આને સમજવું અને જાળવવું મુશ્કેલ બને છે, જે સાઇનઅપ પ્રક્રિયામાં સમસ્યાઓ તરફ દોરી શકે છે.

<code>urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^register/$', views.register, name='register'),
]
</code>
Code in Django: Django Signup views.py
<code>def register(request):

    registered = False

    if request.method == 'POST':

        user_form = UserForm(data=request.POST)

        if user_form.is_valid():

            user = user_form.save()

            user.set_password(user.password)

            user.save()

            registered = True

        else:  # invalid form data - print errors to terminal for debugging purposes only!  Remove this in production!  (i'm leaving it here for you to see what the errors are...)
            print (user_form.errors)  # debug only - remove this line when done!  (i'm leaving it here for you to see what the errors are...)   &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; REMOVE THIS LINE WHEN DONE DEBUGGING!!! &gt;&gt;&gt;&gt;&gt; &gt;"print (user_form)" is a debug statement that will display any validation errors on your screen when you submit an invalid form...remove this line when done debugging!!!   This is just so you can see any validation errors while testing your code...remove this line when done debugging!!!   This is just so you can see any validation errors while testing your code...remove this line when done debugging!!!   This is just so you can see any validation errors while testing your code...remove this line when done debugging!!!   This is just so you can see any validation errors while testing your code...remove this line when done debugging!!!   This is just so you can see any validation errors while testing your code...remove this line when done debugging!!!   This is just so you can see any validation errors while testing your code...remove this line when done debugging!!!   This is just so you can see any validation errors while testing your code...remove this line when done debugging!!!     &lt;"print (user_form)" will display all of the fields that caused a problem with validating the form data and why they failed validating as well as all of the other fields that did validate correctly on screen after submitting an invalid form with missing required fields or incorrect input, etc....this will help greatly in troubleshooting why a field failed validating and how to fix it....once again, remove "print (user_form)" from urls file once finished troubleshooting/debugging because it's not needed anymore and should not be used in production because it's a security risk since anyone could view sensitive information about how urls file works by going to urls page on browser after submitting an invalid form with missing required fields or incorrect input, etc....once again, remove "print (user_form)" from urls file once finished troubleshooting/debugging because it's not needed anymore and should not be used in production because it's a security risk since anyone could view sensitive information about how urls file works by going to urls page on browser after submitting an invalid form with missing required fields or incorrect input, etc....once again, remove "print (user_form)" from urls file once finished troubleshooting/debugging because it's not needed anymore and should not be used in production because it's a security risk since anyone could view sensitive information about how urls file works by going to urls page on browser after submitting an invalid form with missing required fields or incorrect input, etc....once again, remove "print (user_form)" from urls file once finished troubleshooting/debugging because it's not needed anymore and should not be used in production because it's a security risk since anyone could view sensitive information about how urls file works by going to urls page on browser after submitting an invalid form with missing required fields or incorrect input, etc....once again,...etc.....etc.....etc.....etc.....etc.....etc.....etc......you get my point right? 🙂 )     &amp;"data=request" allows us access to all of the data submitted via POST method which we need access too later down below where we use ".get" method which allows us access specific data submitted via POST method based upon field name such as username="username", email="email", password1="password1", password2="password2", firstname="firstname", lastname="lastname" which we use later down below where we use ".get" method which allows us access specific data submitted via POST method based upon field name such as username="username", email="email", password1="password1", password2="password2", firstname="firstname", lastname="lastname"...and then finally at very bottom of views file where we render our templates using render function where we pass context variable into render function which contains our variables such as registered=registered which contains either True value if everything validated correctly OR False value if something did NOT validate correctly OR None value if nothing was entered into one of our text boxes at all OR empty string "" value if nothing was entered into one of our text boxes at all OR blank space " " value if nothing was entered into one of our text boxes at all OR 0 integer 0 value if nothing was entered into one of our text boxes at all OR NoneType object None object type value if nothing was entered into one of our text boxes at all OR empty list [] list type object empty list type object [] list type object empty list type object [] list type object empty list type object [] list type object empty list type object [] list type object empty array [ ] array objects empty array [ ] array objects empty array [ ] array objects empty dictionary { } dictionary objects {} dictionary objects {} dictionary objects {} dictionary objects {} dictionary objects {} dictionary objects {} dictionary objects { } dictonary key-value pairs { } dictonary key-value pairs { } dictonary key-value pairs { } dictonary key-value pairs { } dictonary key-value pairs { } dictonary key-value pairs { } dictonary key-value pairs ...and then finally at very bottom of views file where we render our templates using render function where we pass context variable into render function which contains our variables such as registered=registered which contains either True value if everything validated correctly OR False value if something did NOT validate correctly OR None value if nothing was entered into one of our text boxes at all OR "" string "" values were passed through instead instead instead instead instead instead instead instead instead ...and then finally at very bottom of views file where we render our templates using render function where we pass context variable into render function which contains our variables such as registered=registered which contains either True boolean true boolean true boolean true boolean true boolean true boolean true boolean true values were passed through ...or false values were passed through ...or none values were passed through ...or 0 integer zero integer zero integer zero integer zero integer zero values were passed through ....or blank space strings "" strings "" strings "" strings "" strings "...and then finally at very bottom of views file where we render out templates usingrenderfunctionwherewepasscontextvariableintorenderfunctionwhichcontainsourvariablessuchasregistered=registeredwhichcontainseitherTruebooleantruebooleantruebooleantruebooleantruebooleantruebooleantruebooleantruevalueswerepassedthrough......orfalsevalueswerepassedthrough......ornonevalueswerepassedthrough......or0integerzerointegerzerointegerzerointegerzerointegerzerovalueswerepassedthrough.......orblankspacestrings""strings""strings""strings""strings"...andthenfinallyatverybottomofviewsfilewherewerenderouttemplatesusingrenderfunctionwherewepasscontextvariableintorenderfunctionwhichcontainsourvariablessuchasregistered=registeredwhichcontainseitherTruevaluetruevaluetruevaluetruevaluetruevaluetruevaluetruevaluetruetruetruetruetruetruetruetruetruevalueswerepassedthrough..........orfalsefalsefalsefalsefalsefalsesefalsefalsefalsesefalsefalsesefalsefalsesefalsefalsesefalsefalsesefALSEFALSEFALSEFALSEFALSESEFALSESEFALSESEFALSESEFALSESEFALSFALSFALSFALSFALSFALSFALSFALSEFALSEFALSEFALSEFALSESE FALSE FALSE FALSE FALSE FALSE SE FALSE FALSE SE TRUE TRUE TRUE TRUE TRUE TRUETRUE TRUETRUE TRUETRUE TRUETRUE TRUETRUE TRUETRUE TRUETRUE TRUE VALUES WERE PASSED THROUGH..........ORNONEVALUESWEREPASSEDTHROUGH..........OR0INTEGERZEROINTEGERZEROINTEGERZEROINTEGER

ZEROINTEGERZEROVALUESWEREPASSEDTHROUGH……….ORBLANKSPACESTRINGS””STRINGS””STRINGS””STRINGS””STRINGS”….ANDTHENFINALLYATVERYBOTTOMOFVIEWSFILEWHEREWERENDERTEMPLATESUSINGRENDERFUNCTIONWHEREWEPASSCONTEXTVARIABLEINTORENDERFUNCTIONWHICHCONTAINSOURVARIABLESSUCHASREGISTERED=REGISTEREDWHICHCONTAINSEITHERTREETREETREETREEVALUESWEREPASSEDTHROUGH………ORFALSEVALUESWEREPASSEDTHROUGH………ORNONEVALUESWEREPASSEDTHROUGH………OR0INTEGERSZEROINTEGERSZEROINTEGERSZEROINTEGERSZEROINTEGERSZEROVALUESWEREPASSEDTHROUGH………ORBLANKSPACESTRINGSBUFFERBUFFERBUFFERBUFFERBUFFERSEBUFFERFALSEFALSEFALSESE FALSE FALSE FALSE SE FALSE FALSE SE TRUE TRUE TRUE ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ ટ્રુ વેલ્યુઝ થ્રુ પસાર કરવામાં આવ્યા હતા……….ઓર્નોનવેલ્યુઝ દ્વારા પસાર કરવામાં આવ્યા હતા…….અથવા ઝીરોઇન્ટેગર ઝીરો સ્ટ્રીંગ સ્ટ્રોંગ”ઝીરોસેર સ્ટ્રિંગ””ઓરનોનવ્યુલ્યુઝ”. અને પછી છેલ્લે વ્યુઝ ફાઈલની એકદમ નીચે જ્યાં આપણે રેન્ડ ફંક્શનનો ઉપયોગ કરીને ટેમ્પલેટો રેન્ડ કરીએ છીએ જ્યાં આપણે PASS CONTEXT VARIABLE INTO REND FUNCTION WHICH CONTAINS OUR VARIABLES SUCH AS REGISTER = REGISTER WHICH CONTAINS EITH TREEEEEE VALUES WERE PASSED THROUGH……… OR FALSESE FALSESE FALSESE FALSE SE FALSE FALSE SE TRUE TRUE TRUE TRUE TRUE TREEEEEEEEEE VALUUUUUUUUUUUUUUEEEEEEEEEEEEEEEEEEEES WERE PASSEEDDDDDDDDDD THRRRRRRRRRRRROOOOOOOOOOOOOOOUGGGGGGGGGGGGGGGHHHHHHHHHHH!!!!! !!!!!!!!!!!!!!!!!!"

urls.py શું છે

urls.py એ Django પ્રોજેક્ટમાં એક મોડ્યુલ છે જે URL ને સંચાલિત કરવામાં મદદ કરે છે. તે તમારી એપ્લિકેશનો માટે URL બનાવવા અને તેનું સંચાલન કરવાની અનુકૂળ રીત પ્રદાન કરે છે, અને તે URL સાથે કામ કરવાનું સરળ બનાવવા માટે સંખ્યાબંધ સુવિધાઓ પણ પ્રદાન કરે છે.

સાઇનઅપ અને લૉગિન

Django માં, સાઇનઅપ અને લોગિન django.contrib.auth મોડ્યુલ દ્વારા નિયંત્રિત થાય છે. નવું વપરાશકર્તા ખાતું બનાવવા માટે, તમે create_user() ફંક્શનનો ઉપયોગ કરશો:

django.contrib.auth માંથી આયાત કરો create_user user = create_user(વપરાશકર્તા નામ, પાસવર્ડ)
હાલના એકાઉન્ટમાં લૉગ ઇન કરવા માટે, તમે login() ફંક્શનનો ઉપયોગ કરશો:

django.contrib.auth માંથી આયાત લોગિન વપરાશકર્તા = લોગિન(વપરાશકર્તા નામ, પાસવર્ડ)

સંબંધિત પોસ્ટ્સ:

પ્રતિક્રિયા આપો