【问题标题】:How to write URLs for views.py methods如何为 views.py 方法编写 URL
【发布时间】: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 和一个视图,它只呈现一个模型实例。

标签: django django-urls


【解决方案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 中!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多