最新消息: 新版网站上线了!!!

Django url,从一个页面调到另个页面的方法

´´½¨ÏîÄ¿ºÍÓ¦ÓÃ

django-admin startproject zqxt_views£¨ÏîÄ¿Ãû£©
cd zqxt_views
python manage.py startapp calc£¨Ó¦ÓÃÃû£©

¡ª¡ª¡ª¡ª¨C1.²ÉÓÃ/add?a=4&b=5ÕâÑùGET·½·¨½øÐСª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª

ÐÞ¸Ä calc/view.pyÎļþ

from django.shortcuts import render
from django.http import HttpResponse

def add(request):
 a = request.GET['a']
 b = request.GET['b']
 c = int(a)+int(b)
 return HttpResponse(str(c))

ÐÞ¸Ä zqxt_views/urls.py Îļþ£¬Ìí¼ÓÒ»¸öÍøÖ·À´¶ÔÓ¦ÎÒÃǸղÅн¨µÄÊÓͼº¯Êý¡£

from django.conf.urls import url
from django.contrib import admin
from calc import views as calc_views

urlpatterns = [
 url(r'^add/$', calc_views.add, name='add'), # ×¢ÒâÐÞ¸ÄÁËÕâÒ»ÐÐ
 url(r'^admin/', admin.site.urls),
]

´ò¿ª·þÎñÆ÷²¢·ÃÎÊ

python manage.py runserver 8003

´ò¿ªÍøÖ·

http://127.0.0.1:8003/add/?a=4&b=5

¡ª¡ª¡ª¡ª¨C2.²ÉÓÃ/add/3/4ÕâÑùµÄÍøÖ·µÄ·½Ê½¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª

ÐÞ¸Ä calc/views.py Îļþ£¬ÔÙ¶¨ÒåÒ»¸öadd2º¯Êý

def add2(request, a, b):
 c = int(a) + int(b)
 return HttpResponse(str(c))

½Ó×ÅÐÞ¸Ä zqxt_views/urls.py Îļþ£¬ÔÙÌí¼ÓÒ»¸öÐ嵀 url

url(r'^add/(\d+)/(\d+)/$',calc_views.add2,name='add2'),

ÎÒÃÇ¿ÉÒÔ¿´µ½ÍøÖ·ÖжàÁË (\d+), ÕýÔò±í´ïʽÖÐ \d ´ú±íÒ»¸öÊý×Ö£¬+ ´ú±íÒ»¸ö»ò¶à¸öÇ°ÃæµÄ×Ö·û£¬Ð´ÔÚÒ»Æð \d+ ¾ÍÊÇÒ»¸ö»ò¶à¸öÊý×Ö£¬ÓÃÀ¨ºÅÀ¨ÆðÀ´µÄÒâ˼ÊDZ£´æΪһ¸ö×Ó×飬ÿһ¸ö×Ó×齫×÷Ϊһ¸ö²ÎÊý£¬±» views.py ÖеĶÔÓ¦ÊÓͼº¯Êý½ÓÊÕ¡£

ÎÒÃÇÔÙ·ÃÎÊ http://127.0.0.1:8003/add/4/5/ ¾Í¿ÉÒÔ¿´µ½ºÍ¸Õ²ÅͬÑùµÄЧ¹û£¬µ«ÊÇÕâ»ØÍøÖ·¸üÓÅÑÅÁË

´ò¿ª zqxt_views/urls.py

from django.conf.urls import url
from django.contrib import admin
from calc import views as calc_views

urlpatterns = [
 url(r'^admin/', admin.site.urls),
 url(r'^add/$',calc_views.add,name='add'),
 url(r'^add/(\d+)/(\d+)/$',calc_views.add2,name='add2'),
]

ÔÚ url(r'^add/$',calc_views.add,name='add'),ÖУ¬name='add'ÊÇÓÃÀ´¸ÉʲôµÎ°¡£¿

¼òµ¥Ëµ£¬name¿ÉÓÃÓÚÔÚtemplates,models£¬views¡­¡­Öеõ½¶ÔÓ¦µÄÍøÖ·£¬Ï൱ÓÚ¸ø¡°ÍøÖ·È¡ÁËÒ»¸öÃû×Ö¡±£¬Ö»ÒªÃû×Ö²»±ä£¬ÍøÖ·±äÁËÒ²ÄÜͨ¹ýÃû×Ö»ñÈ¡µ½¡£

ΪÁ˼ÌÐøŪÇå³þÕâ¸öÎÊÌ⣬ÎÒÃÇÏȽ¨Á¢Ò»¸öÊ×Ò³µÄÊÓͼºÍurl

ÐÞ¸Ä *calc/views.py*

from django.http import HttpResponse
from django.shortcuts import render


def index(request):
 return render(request, 'home.html')

render ÊÇäÖȾģ°å

½«¡®calc'Õâ¸öapp¼ÓÈëµ½*zqxt_views/settings.py ÖÐ*

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

 'calc',
]

ÕâÑù£¬Ê¹ÓÃrenderµÄʱºò£¬Django»á×Ô¶¯ÕÒµ½INSTALLED_APPSÖÐÁгöµÄ¸÷¸öappϵÄtemplatesÖеÄÎļþ¡£

Ìáʾ£ºDEBUG=TrueµÄʱºò£¬Django¿ÉÒÔ×Ô¶¯ÕÒµ½¸÷¸öappÏÂstaticÎļþÖеľ²Ì¬Îļþ£¨js,css£¬Í¼Æ¬£¬×ÊÔ´£©

ÎÒÃÇÔÚcalcÕâ¸öappÖÐн¨Ò»¸ötemplatesÎļþ¼Ð£¬ÔÚtemplatesÖÐн¨Ò»¸öhome.html

Îļþ calc/templates/home.html ÖÐдÈëÒÔÏÂÄÚÈÝ

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>title</title>
</head>
<body>
 <a href="/add/4/5" rel="external nofollow" >¼ÆËã 4 + 5 </a>
</body>
</html>

ÐÞ¸Äzqxt_views/urls.py

urlpatterns = [
 url(r'^$', calc_views.index, name='home'),
 url(r'^add/$', calc_views.add, name='add'),
 url(r'^add/(\d+)/(\d+)/$', calc_views.add2, name='add2'),
 url(r'^admin/', admin.site.urls),
]

ÔËÐзþÎñÆ÷python manage.py runserver 8004£¬·ÃÎÊhttp://127.0.0.1:8004/

ÎÒÃǼÆËã¼Ó·¨µÄʱºòÓõÄÊÇ /add/4/5/ £¬ºóÀ´ÐèÇó·¢Éú±ä»¯£¬±ÈÈç¸Ä³É /4_add_5/£¬µ«ÔÚÍøÒ³ÖУ¬´úÂëÖкܶàµØ·½¶¼Ð´ËÀµÄ /add/4/5/£¬±ÈÈçÄ£°åÖпÉÄÜÊÇÕâôдµÄ

<a href="/add/4/5/" rel="external nofollow" rel="external nofollow" >¼ÆËã 4+5</a>

Èç¹ûÕâÑùд¡°ËÀÍøÖ·¡±£¬»áʹµÃÔÚ¸ÄÁËÍøÖ·£¨ÕýÔò£©ºó£¬Ä£°å£¨template)£¬ÊÓͼ(views.py£¬±ÈÈçÓÃÓÚURLÌøת)£¬Ä£ÐÍ(models.py£¬»ñÈ¡¼Ç¼·ÃÎʵØÖ·µÈ£©ÓÃÁË´ËÍøÖ·µÄ£¬¶¼±ØÐë½øÐÐÏàÓ¦µÄ¸ü¸Ä£¬Ð޸ĵĴú¼ÛºÜ´ó£¬Ò»²»Ð¡ÐÄ£¬Óеĵط½Ã»¸Ä¹ýÀ´£¬¾Í²»ÄÜÓÃÁË¡£

reverse ½ÓÊÕ url ÖÐµÄ name ×÷ΪµÚÒ»¸ö²ÎÊý£¬ÎÒÃÇÔÚ´úÂëÖоͿÉÒÔͨ¹ý reverse() À´»ñÈ¡¶ÔÓ¦µÄÍøÖ·

ÎÒÃÇÔÚÖÕ¶ËÉÏÊäÈ룺python manage.py shell

>>> from django.core.urlresolvers import reverse # django 1.4.x - django 1.10.x

»òÕß

>>> from django.urls import reverse # Django 1.10.x - Django 2.x еģ¬¸ü¼Ó¹æ·¶ÁË

>>> reverse('add2', args=(4,5))
u'/add/4/5/'
>>> reverse('add2', args=(444,555))
u'/add/444/555/'

ÔÚÍøҳģ°åÖÐÒ²ÊÇÒ»Ñù£¬¿ÉÒԺܷ½±ãµÄʹÓá£

²»´ø²ÎÊýµÄ£º
{% url 'name' %}
´ø²ÎÊýµÄ£º²ÎÊý¿ÉÒÔÊDZäÁ¿Ãû
{% url 'name' ²ÎÊý %}

Òò´Ë£¬ÎÒÃǽ«£ºtemplates/home.html ÖÐ

<a href="/add/4/5/" rel="external nofollow" rel="external nofollow" >link</a>¸Ä³É
<a href="{% url 'add2' 4 5 %}" rel="external nofollow" >link</a>

ÕâÑùµÄ»°£¬µ± urls.py ½øÐиü¸Ä£¬Ç°ÌáÊDz»¸Ä name£¨Õâ¸ö²ÎÊýÉ趨ºÃºó²»ÒªÇáÒ׸ģ©£¬»ñÈ¡µÄÍøÖ·Ò²»á¶¯Ì¬µØ¸ú×ű䣬±ÈÈç¸Ä³É£º

url(r'^new_add/(\d+)/(\d+)/$', calc_views.add2, name='add2'),

{% url ¡®add2' 4 5 %}¾Í»á±»äÖȾ³É/new_add/4/5/

ÓÐÁíÍâÒ»¸öÇé¿ö£¬Èç¹ûÎÒÃÇÔ­À´ÊղصÄÒ»¸öÍøÖ·£¬ÏÖÔÚÕâ¸öÍøÖ·±»·ÏÁË£¬ÏÖÔÚÓиöеÄÍøÖ·£¬ÈçºÎÈÃÒÔÇ°µÄ/add/3/4×Ô¶¯Ìøתµ½ÐµÄÍøÖ·ÄÇ£¿

¾ßÌåµÄ˼·ÊÇ£¬ÔÚview.pyдһ¸öÌøת·½·¨£º

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

def old_add2_redirect(request, a, b):
 return HttpResponseRedirect(
 reverse('add2', args=(a, b))
 )

ÒÔÉÏÕâƪDjango url,´ÓÒ»¸öÒ³Ãæµ÷µ½Áí¸öÒ³ÃæµÄ·½·¨¾ÍÊÇС±à·ÖÏí¸ø´ó¼ÒµÄÈ«²¿ÄÚÈÝÁË£¬Ï£ÍûÄܸø´ó¼ÒÒ»¸ö²Î¿¼£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö½Å±¾Ö®¼Ò¡£

转载请注明:谷谷点程序 » Django url,从一个页面调到另个页面的方法