进不到/buy/id,id指的是数据库里面序号pk
1.urls.py
from django.contrib import admin
from django.urls import path
from website import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
path(r'buy/(?P<gid>\d+)/', views.buy),
]
2.views.py
def buy(request, gid):
obj = models.Goods.objects.get(gid=pk)
print(obj.name, obj.price, gid)
no = str(uuid.uuid4())
models.Order.objects.create(
no=no,
goods_id=obj.id
)
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/buy/1/
Using the URLconf defined in Alipay_Final.urls, Django tried these URL patterns, in this order:
admin/
index/
buy/(?P<gid>\d+)/
The current path, buy/1/, 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.
from django.urls import path, re_path
rlpatterns = [
path('articles/2003/', views.special_case_2003),
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
]
rom django.urls import path
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]
看看你需要的是哪种,