Django url 问题无法加载我的 html 模板
Django url problems not loading my html template
*我已经在其中创建了一个商店应用 *
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,但只有主/商店页面出现,没有其他 url 像 /shop/product 或 /shop/cart 他们都没有工作我已经尝试了很长时间了
首先,您不应该通过 .html
访问页面,它应该只能通过 url 访问。
您已经在此处使用 shop/
:
path('shop/', include('shop.urls')),
所以你应该避免在下面的子urls中使用它。
否则您将必须通过 shop/shop/cart/
而不是 shop/cart/
.
访问
Change the urls as below:
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"),
]
这应该适合你。
*我已经在其中创建了一个商店应用 *
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,但只有主/商店页面出现,没有其他 url 像 /shop/product 或 /shop/cart 他们都没有工作我已经尝试了很长时间了
首先,您不应该通过 .html
访问页面,它应该只能通过 url 访问。
您已经在此处使用 shop/
:
path('shop/', include('shop.urls')),
所以你应该避免在下面的子urls中使用它。
否则您将必须通过 shop/shop/cart/
而不是 shop/cart/
.
Change the urls as below:
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"),
]
这应该适合你。