February 13th 2020 1233
In this tutorial, we will learn what are URLs in Django and how to create them and link them with the view functions that we have created in views.py file.
So in our previous TUTORIAL, we have successfully created our Templates. Let's create URLs for each view Functions which is the last step to render our Html templates to display them in the browser.
We want to access Signup view via a URL. Django has his own way for URL mapping and it's done by editing your project
urls.py file (social/urls.py).
When a user makes a request for a page on your web app, Django controller takes over to look for the corresponding view via the url.py file, and then return the HTML response or a 404 not found error, if not found. In url.py, the most important thing is the "urlpatterns" tuple. It’s where you define the mapping between URLs and views. A mapping is a tuple in URL patterns, the
url.py file looks like −
from django.contrib import admin from django.urls import path from django.conf.urls import url from account import views as acccount_views urlpatterns = [ path('admin/', admin.site.urls), url(r"^$", acccount_views.IndexView.as_view(), name="index"), url(r"^signup/$", acccount_views.signup, name="signup"), ]
add this line in the bottom of
LOGIN_REDIRECT_URL = 'index'
Now run the following command where the
manage.py file is located:
python manage.py runserver
You will see the home page click on the signup button to go to the Signup page.
Fill the form and if everything is alright then you will we redirected to the home page as logged in user.
Congratulation! We have successfully implemented the signup view in Django.