Today, I will take a new part of Python Django from my tutorial series. In this part you will learn the Database model features.
I already told you the following:
- Firstly create django project with django app.
- Now run django project using open command prompt in root directory and write command : python manage.py runserver.
- You have successfully run your django project.
- Now open views.py file in django app folder.
- Write code in views.py as below
from django.http import HttpResponse,
# Create your views here.
def firstView(request):
data = "<html><body>Hello is new view of django<body></html>"
return HttpResponse(data)
- Now open urls.py in django project root folder.
- Write code as below.
from django.conf.urls import url
urlpatterns = [
url(r'^$', 'main.views.firstView', name='home'),
]
- After this refresh browser then you will see as below:
- According to views, file code defines the method that has data variable content and some string that will convert by browser.
- HttpResponce is used for returning to page. When you want see results in any page use any function that must return the type of response.
- In URLS.py file code define some part like: urlpattern is use for write URL handling in this array. This is use for store web page URL.
- URL method contains first parameter that defines web page URL which works as regular expression.
- Second parameter ‘main.views.firstView’ is use for locating fisrtView method that we will use when this url hits on browser and last parameter is used for giving unique name for identifying particular url and you can use this in view file.
- Now I will show another example of handling url and views.
- Now make another method in views.py as below.
from django.conf.urls import url
urlpatterns = [
url(r'^$', 'main.views.firstView', name='home'),
url(r'^name/$', 'main.views.askName', name='name').
]
- Write code in urls.py as below,
from django.conf.urls import url
urlpatterns = [
url(r'^$', 'main.views.firstView', name='home'),
url(r'^name/$', 'main.views.askName', name='name'),
]
- Now open browser and go to URL http://127.0.0.1:8000/name/
- You will see output as below image:
Django framework provides many features for handling url and view as dynamic.
Django most important feature is that django also has tag for using in html page as angularJs.But its tag only runs only once when page is loading.