According to Django framework, a website or a webproject consists of one or more web app.
We can make several module(app) to run a website.For instance, for making a blogging website we can have login module(app),blogging module(app),personal information module(app) like about,contact . In that case our webproject will have three app , login app, bloggin app, personal app.
webproject ├── webproject │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── loginapp │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ ├── models.py │ ├── tests.py │ └── views.py ├── bloggingapp │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ ├── models.py │ ├── tests.py │ └── views.py ├── personalapp │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ ├── models.py │ ├── tests.py │ └── views.py └── manage.py
Currently we only have default webproject called mysite and our current project sturcture is like below
mysite/ ├── mysite/ │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py
We will create an app inside our webproject which will be named as webapp.
To do this lets execute following command to
python manage.py startapp webapp
Now a new directory exists, called webapp. In here, we see a lot of similar files, and some new ones.
mysite/ ├── mysite/ -our main project file │ ├── __init__.py │ ├── settings.py │ ├── urls.py - our project's main router file │ └── wsgi.py ├── webapp -newly created app │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ ├── models.py │ ├── tests.py │ └── views.py └── manage.py
If you familiar with dot net MVC then you will understand that a project consists of Model -> used for data
View -> used for templeting
Controller -> used for all funtionlalities
Like MVC pattern of dot net, django also consists of views,models but ift also has some extra things like url .
For simplicity, we will show a html header to the user. For this let’s modify the
from django.shortcuts import render from django.http import HttpResponse def index(request): #We have defined a view named index return HttpResponse("<h2>HEY!</h2>") #will return some html
We have defined a view named index which just returns some html code to show,in that case “<h2>HEY!</h2>“.
Somehow,somewhere we will call this index view .
But question is when and how??
What will be the url??
To show something(view) to the user we have to go to some url that’s why we need to define the url to show this html.
So let’s create a url file.
Our new structure will be:
mysite/ ├── mysite/ │ ├── __init__.py │ ├── settings.py │ ├── urls.py --contains project's main urls │ └── wsgi.py ├── webapp/ │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ ├── models.py │ ├── tests.py │ └── views.py │ └── urls.py --contains webapp urls └── manage.py
So , urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), ]
Here, urlpatterns defines how and what will be displayed.
Our expression here is
^$. The carrot marks that it is the start of a string, then the $ marks the end. Thus, this specific URL will be the “index,” where there will be nothing else in the URL, like a homepage.
This url calls a view named index which was previously created.
So, now our webapp has url handling, but our main project actually doesn’t point to the webapp at all, so let’s do that next.
If we open our main project’s urls.py which is our main router/url file.
from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), ]
We’ll leave the admin there, and talk about that later, but basically Django already has a built-in admin control panel. It means if we goto http://127.0.0.1:8000/admin it will be executed. Let’s forget about this right for the time being.
We want to point out our webapp’s url to our main project. We have to define the url like below:
from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^webapp/', include('webapp.urls')), ]
So, whenever a user will goto http://127.0.0.1:8000/webapp/
then it will search webapp.urls file to match regular expression.
As http://127.0.0.1:8000/webapp has no more url extension so it matchs url(r‘^$‘, views.index, name=‘index’) from webapp/urls.py and will return the index view.
If we had http://127.0.0.1:8000/webapp/about then we would need to define a url like
url(r‘^about$‘, views.about, name=’about’) .
Next, we need to finally include our new app in our installed applications:
# ...this is just a slice of code within settings.py # do not delete the other code # just add 'webapp' to the list. INSTALLED_APPS = [ 'webapp', #this will add the newly created app to the main project 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
Here, we’ve got our main project pointing to our webapp, starting at /webapp/. Thus, what will happen next is Django will bop over to the webapp dir, reference the
urls.py file there, and continue. In this case, /webapp/ will be treated as the “root,” so if you have a url in your webapp that is just empty, like we do, then to get there, you would type in
yourwebsite.com/webapp/, and that would get you there. Go ahead and test that. Head to your console, making sure you are working in you project’s main container directory (the one containing
manage.py), and run:
python manage.py runserver
You should be greeted with your screaming “HEY!”
Note that, if you left the runserver command still going, then the changes should be live already.
Now if you want to show something to this url http://127.0.0.1:8000/webapp/welcome then create a new regular expression to match the url to the file of
from django.conf.urls import url
from . import views
urlpatterns = [
url(r’^$’, views.index, name=’index’),
url(r’^welcome$’, views.welcome, name=’welcome’), #newly defined url
Then define a new view to the webapp/views.py
from django.shortcuts import render
from django.http import HttpResponse
def welcome(request): #newly defined view
return HttpResponse(“<h2>Welcome !! you are doing great so far.</h2>”)
You will see this: