**我现在已经在其中创建了一个商店应用程序*
shop / urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="ShopHome"),
path("shop/cart/", views.cart, name="Cart"),
path("shop/checkout/", views.checkout, name="Checkout"),
path("shop/contact/", views.contact, name="ContactUs"),
path("shop/register/", views.register, name="Register"),
path("shop/product_details/", views.product_details, name="ProductDetails"),
path("shop/products/", views.products, name="Products"),
]
主项目urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'shop/index.html')
def register(request):
return render(request, 'shop/register.html')
def contact(request):
return render(request, 'shop/contact.html')
def products(request):
return render(request, 'shop/products.html')
def product_details(request):
return render(request, 'shop/product_details.html')
def cart(request):
return render(request, 'shop/cart.html')
def checkout(request):
return render(request, 'shop/checkout.html')
models.py
from django.db import models
# Create your models here.
class Product(models.Model):
name = models.CharField(max_length=300)
slug = models.SlugField(max_length=150)
description = models.TextField()
image = models.ImageField(upload_to='shop/images', default='')
manufacturer = models.CharField(max_length=300, blank=True)
price_in_dollars = models.DecimalField(max_digits=6, decimal_places=2)
def __str__(self):
return self.name
setting.py已安装的应用程序部分
INSTALLED_APPS = [
'shop.apps.ShopConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
apps.py
from django.apps import AppConfig
class ShopConfig(AppConfig):
name = 'shop'
admin.py
from django.contrib import admin
# Register your models here.
from . models import Product
admin.site.register(Product)
* HTML未在浏览器中加载,显示为*
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/shop/products.html
Using the URLconf defined in mac.urls, Django tried these URL patterns, in this order:
admin/
shop/ [name='ShopHome']
shop/ cart/ [name='Cart']
shop/ checkout/ [name='Checkout']
shop/ contact/ [name='ContactUs']
shop/ register/ [name='Register']
shop/ product_details/ [name='ProductDetails']
shop/ products/ [name='Products']
The current path, shop/products.html, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
[我已经尝试了许多其他的html,但是只有/ shop主页面似乎没有其他url在起作用,就像/ shop / product或/ shop / cart一样,它们中的任何一个都无法正常工作,因为我已经尝试了很长时间了
首先,您不应该通过.html
访问页面,而只能通过url访问。
您已经在这里使用shop/
:
path('shop/', include('shop.urls')),
因此,您应该避免在下面的子网址中使用它。
否则,您将必须通过shop/shop/cart/
而不是shop/cart/
进行访问。
更改网址如下:
urlpatterns = [
path("", views.index, name="ShopHome"),
path("cart/", views.cart, name="Cart"),
path("checkout/", views.checkout, name="Checkout"),
path("contact/", views.contact, name="ContactUs"),
path("register/", views.register, name="Register"),
path("product_details/", views.product_details, name="ProductDetails"),
path("products/", views.products, name="Products"),
]
这应该适合您。