程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

URL reverse parsing, static file configuration and application creation of Django framework

編輯:Python

List of articles

  • Django Of URL Reverse DNS
    • Used in template layer
    • Use in view functions
  • Django Configuration of static files
  • Django Configuration of application
    • Create an
    • Distributed routing
    • To configure templates

Django Of URL Reverse DNS

In the web page URL The embedding of is essential ,URL There are absolute address writing and relative address writing , add URL It may become very complicated in a complex project , So we can use django Medium URL Reverse parsing method , by url Take the alias , Then you can directly use the alias to access .

Used in template layer

First configuration urls.py:

from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path("index", views.index),
path("test_url_back/<int:number>", views.test_url_back, name="url1")
]

among path Medium name The parameter is equivalent to url Take the alias , At that time HTML The corresponding routing address can be obtained by directly calling .

views.py

from django.shortcuts import render
from django.http import HttpResponse
def test_url_back(request, number):
html = str(number) + "hello back url"
return HttpResponse(html)
def index(request):
return render(request, "index.html")

templates in index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="{% url "url1" "10086"%}">url Reverse DNS </a>
</body>
</html>

{%url + Need to jump url Another name for + Match the passed in parameters %}

Then run the server :

Click the button to jump to :

Use in view functions

Import required from django.urls import reverse
To be able to use

Here is a small example of configuring an access and then jumping to another page .

First urls.py:

from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path("index", views.index, name="base_index"),
path("test_url_back/<int:number>", views.test_url_back, name="url1"),
path("new", views.new, name="new_html")

views.py:

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
def test_url_back(request, number):
url = reverse("new_html")
# Respond to requests and go to url The routing 
return HttpResponseRedirect(url)
def index(request):
return render(request, "index.html")
def new(request):
return render(request, "new.html")

new.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
hello world
</body>
</html>

Then start the server , visit http://127.0.0.1:8000/index

Place the mouse over the button to show jump to http://127.0.0.1:8000/test_url_back/10086 page
After clicking the button

Will automatically jump to new page .

Django Configuration of static files

The first thing you need to do is setting.py Configure static file routing in :

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), )

STATIC_URL = '/static/' stay setting.py The default is , Just add the following line , Let's go django Know the location of the static file .

Then create static Folder and sub files, and put the resources to be called into

Then you can call the static file .

Django It also provides a relatively simple way

{
%load static%} # Write this first 
{
% static “ Static resource path ”%} # You can then call... In this way 

In this way , Even if the setting.py The static file name in has changed ,django You can still successfully find resources , More applicable .

Let's start to configure the static file .
urls.py:

from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path("index", views.index, name="base_index"),
]

views.py:

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
def index(request):
return render(request, "index.html")

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
hello world!
<img src="/static/img/log_left_up.png" width="50%" height="50%">
{%load static%}
<img src="{%static 'img/log_left_up.png'%}" width="50%" height="50%">
</body>
</html>

Then start to be able to load the picture to indicate that the static resource access is successful

Django Configuration of application

Create an

Create an , It is equivalent to modularizing web page management , Every app has its own MTV Pattern , Easy to manage and maintain .

The following is an example while explaining , For example, to create a sports Application :

Create an Python manage.py startapp apply name

Python manage.py startapp sports

The generated file directory is as follows :

Migrations Usually, the model migration files of the layer where the model layer deals with the database are placed .
Admin.py It is related to the management background
Apps.py Relevant configurations under the application
Models,py The entrance of the model layer , Database related
Tests.py Test entrance
Views.py The view function

And then in setting.py Of INSTALLED_APPS List to install this app

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sports'
]

Distributed routing

Now that you can use applications for modular management , Then we can use distributed routing to facilitate our management .

Django Master routing profile in urls.py You don't have to deal with user specific routing , The main route configuration file can be used to distribute requests , Specific requests can be handled by the routing configuration files in their respective applications .

So we can configure distributed routing

Master routing file urls.py:

from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path("sports/", include("sports.urls"))
]

Now the main route only needs to assign the route management to sports Under the urls.py Configuration file management , It is not responsible for the jump of all routing addresses .

Sports Create under folder urls.py:

from django.urls import path
from . import views
urlpatterns = [
path("sport_index", views.index),
]

Here, you need to configure specific routing addresses , Then the complete access route is the primary route + Sub route :http://127.0.0.1:8000/sports/sport_index

Sports Under the views.py:

from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("hello world sports!!!")
# Create your views here.

Then run the server , visit http://127.0.0.1:8000/sports/sport_index
You can see

This indicates that the distributed routing configuration is successful !

To configure templates

First create templates Folder
And then again settings.py Open the template function in ( Default on )
Under the application of templates And outer layer templates When they all exist ,django The search rules are as follows :

  1. Find the outer layer first templates Template under directory
  2. Press INSTALLED_APPS The application order under configuration is searched layer by layer .

First of all sports Create under file templates Folder
Then build sport_new.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
hello world i am from app_sports
</body>
</html>

Sports Under the urls.py:

from django.urls import path
from . import views
urlpatterns = [
path("sport_index", views.index),
]

Sports Under the views.py:

from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'sport_new.html')
# Create your views here.


You can see that the configuration is successful


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved