我无法将视图链接到我正在构建的电子商务模板中的基本HttpResponse。
我得到的错误是404,但经过这里查看以及文档说我对我错过的内容感到有些困惑,
模型
from django.db import models
class Category(models.Model):
parent = models.ForeignKey('self', null=True, blank=True)
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=150)
description = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
mod_date = models.DateTimeField(auto_now=True)
class Manufacturer(models.Model):
name = models.CharField(max_length=150)
slug = models.SlugField(max_length=150)
class Product(models.Model):
category = models.ForeignKey(Category)
manufacturer = models.ForeignKey(Manufacturer)
name = models.CharField(max_length=300)
slug = models.SlugField(max_length=150)
description = models.TextField()
photo = models.ImageField(upload_to='itemphotos')
price_in_sterling = models.DecimalField(max_digits =6, decimal_places=2)
available = models.BooleanField()
instock = models.IntegerField()
pub_date = models.DateTimeField(auto_now_add=True)
mod_date = models.DateTimeField(auto_now=True)
我的views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hi, your view worked")
def catagory(request):
return HttpResponse("Hi, you are looking at the catagory landing page")
def manufacturer(request):
return HttpResponse("Hi, here you can shop by brand")
def product(request):
return HttpResponse("Hi, here you can search by product")
我的urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^productcatalog/', include('productcatalog.urls')),
url(r'^admin/', include(admin.site.urls)),
]
来自app produtcatalog的我的urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^productcatalog/$', views.catagory, name='catagory'),
url(r'^productcatalog/$', views.manufacturer, name='brand'),
url(r'^productcatalog/$', views.product, name='products'),
]
我确定错误发生在第二个urls.py文件中,但似乎无法捕获它。
答案 0 :(得分:0)
我有另一种方法
urls.py
from views import product
urlpatterns=[url(r'^product/$', product)]
views.py
def product(request):
view="""<html><head></head><body><p>something</p></body></html> """
return HttpResponse(view)
我不记得我是否使用了渲染方法,但在将变量放入html页面时非常有用
答案 1 :(得分:0)
您正在为所有视图功能使用相同的网址。尝试更改它。
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^productcatalog-1/$', views.catagory, name='catagory'),
url(r'^productcatalog-2/$', views.manufacturer, name='brand'),
url(r'^productcatalog-3/$', views.product, name='products'),