如何为 views.py 方法编写 URL
Posted
技术标签:
【中文标题】如何为 views.py 方法编写 URL【英文标题】:How to write URLs for views.py methods 【发布时间】:2015-10-29 08:47:35 【问题描述】:我想在 html 页面中显示所有详细信息,但我的问题在于我的 urls.py
。它只显示第一个 URL,而不显示后面的 URL。
views.py
from django.shortcuts import render
from .models import Name, Description, WorkType, Hours, Price, PricePeriod,
DateCreated, DateModified
def name(request):
name = Name.name_text
context = 'name': name
return render(request, 'poc_html/index.html', context)
def description(request):
description = Description.description_text
context = 'description': description
return render(request, 'poc_html/index.html', context)
def worktype(request):
worktype = WorkType.worktype_text
context = 'worktype': worktype
return render(request, 'poc_html/index.html', context)
def hours(request):
hours = Hours.hours_text
context = 'hours': hours
return render(request, 'poc_html/index.html', context)
def price(request):
price = Price.price_text
context = 'price': price
return render(request, 'poc_html/index.html', context)
def priceperiod(request):
priceperiod = PricePeriod.priceperiod_text
context = 'priceperiod': priceperiod
return render(request, 'poc_html/index.html', context)
def datecreated(request):
datecreated = DateCreated.datecreated.text
context = 'datecreated': datecreated
return render(request, 'poc_html/index.html', context)
def datemodified(request):
datemodified = DateModfied.datemodified_text
context = 'datecreated': datemodified
return render(request, 'poc_html/index.html', context)
urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.name, name='name'),
url(r'^$', views.description, name='description'),
url(r'^$', views.worktype, name='worktype'),
url(r'^$', views.price, name='price'),
url(r'^$', views.priceperiod, name='priceperiod'),
url(r'^$', views.hours, name='hours'),
url(r'^$', views.datecreated, name='datecreated'),
url(r'^$', views.datemodified, name='datemodified'),
]
【问题讨论】:
您所有的urlpatterns
都捕获了相同的内容,只有一个空 URL。请阅读教程(参见例如docs.djangoproject.com/en/1.8/intro/tutorial03),您显然完全不知道自己在做什么。
实际上,所有这些模式都只捕获空 URL。但我认为问题远不止于此。我非常怀疑 OP 是否需要单独的页面来显示名称、描述、工作类型等;请注意它们如何呈现相同的模板。实际上看起来他想要一个 URL 和一个视图,它只呈现一个模型实例。
【参考方案1】:
您需要修改 url 以导航到正确的视图定义。 您可以执行以下操作:
urls.py
from django.conf.urls import url,include
from . import views
urlpatterns = [
url(r'^name/$', views.name, name='name'),
url(r'^description/$', views.description, name='description'),
url(r'^worktype/$', views.worktype, name='worktype'),
url(r'^price/$', views.price, name='price'),
url(r'^priceperiod/$', views.priceperiod, name='priceperiod'),
url(r'^hours/$', views.hours, name='hours'),
url(r'^datecreated/$', views.datecreated, name='datecreated'),
url(r'^datemodified/$', views.datemodified, name='datemodified'),
]
【讨论】:
【参考方案2】:当然,像这样改变你的 url 模式:
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/([0-9]4)/$', views.year_archive),
url(r'^articles/([0-9]4)/([0-9]2)/$', views.month_archive),
url(r'^articles/([0-9]4)/([0-9]2)/([0-9]+)/$', views.article_detail),
]
为每个视图添加唯一路线
或者将所有数据合并到 ONE route + view 中!
【讨论】:
以上是关于如何为 views.py 方法编写 URL的主要内容,如果未能解决你的问题,请参考以下文章
Ajax URL 未在 views.py 中执行正确的功能,如 urls.py 中所定义
04-在views.py中使用class编写django项目
如何使用从views.py发送的变量将几个参数传递给url模板标签