Django Tutorial
What is Django?
How Django Works?
Django - Creating a Project
Django- Creating an App
Django- Models
Django- Forms
Django – Views
Django- Templates
Django- URLs
Django- Admin
Complete Authentication System
Setting Up Django Environment Setup
Altaf Shaikh

BE Engg | Python Dev | Content Writer | Founder of TeachMeBro | Blockchain Dev | Django

Django- URLs

February 13th 2020

What are Django URLs?

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.

Django URLs

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 social/settings.py

LOGIN_REDIRECT_URL = 'index'

Now run the following command where the manage.py file is located:

python manage.py runserver

goto http://127.0.0.1:8000/

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.