Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Creating Django index.html
- Assume a directory structure such as the following:
- ~/workspace/mysite/mysite
- ~/workspace/mysite/siteapp
- Here is an explanation of how views work.
- 1) The url matches in the mysite/urls.py
- 2) any matching part is stripped off, and sent to the siteapp/urls.py
- 3) a view is returned based on this match. This view is actually a function that returns
- an HTML template (html with variables/programming logic).
- 4) We need to create the template to make sure it exists.
- //////////////////// 1 ///////////////////////////
- In the mysite/urls.py add the following lines
- from django.conf.urls import url, include
- urlpatterns = [
- url(r'^mysite/', include("siteapp.urls")),
- ]
- ///////////////////// 2 ////////////////////////////
- If a user types in https://www.domain.com/mysite, django will
- send https://www.domain.com to siteapp/urls.py file for further processing.
- Firstly, make sure this file exists,
- $ touch ~/workspace/mysite/siteapp/urls.py
- Then add the following lines
- from django.conf.urls import url
- from . import views
- urlpatterns = [
- url(r'^$', views.index, name='index')
- ]
- ////////////// 3 /////////////////////////
- Next, we need to make sure that ~/workspace/mysite/siteapp/views.py
- contains a function named index. Add the following lines to that file,
- from django.shortcuts import render
- # Create your views here.
- def index(request):
- return render(request, 'siteapp/index.html', {'something': 'World!'})
- ////////////////// 4 ///////////////////////
- The above function returns an html template located in ~/workspace/mysite/siteapp/templates/siteapp/index.html
- We need to make sure this file exists. The function also substitutes all occurrences of the variable "{{ something }}"
- with "World!".
- Create the directory if it does not exist,
- $ mkdir -p ~/workspace/mysite/siteapp/templates/siteapp/
- Then add the following lines,
- <!DOCTYPE HTML>
- {% extends "base.html" %}
- <html>
- <head>
- <title>MySite!</title>
- </head>
- <body>
- <p>Hello {{ something }}</p>
- </body>
- </html>
- ////////////////////////////////////////////////
- Go ahead and run the dev server and visit the page to make sure it works.
- $ ./manage.py runserver 0.0.0.0:80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement