如何为views.py方法

时间:2015-08-06 09:21:19

标签: django django-urls

我想在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'),
]

2 个答案:

答案 0 :(得分:2)

您需要修改网址以导航到正确的视图定义。 您可以执行以下操作:

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'),
]

答案 1 :(得分:0)

当然,请更改您的网址格式:

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),
] 

为每个视图添加唯一路线

或者在一个路线+视图中组合所有数据!