gpt4 book ai didi

python - 通过给 django 提供 url 无法打开页面

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:45 25 4
gpt4 key购买 nike

我无法通过在浏览器上输入 URL“localhost:8000/purchasing”来显示购买模板。我是这里的初学者。

  • 项目表格
    • url.py
  • 预订表格
    • url.py
    • views.py

projectforms.urls

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import include, url

urlpatterns = [
path('admin/', admin.site.urls),
path('',include('bookingform.urls')),
path('purchasing/', include('bookingform.urls')),
]

bookingform.url

from django.urls import path
from . import views


urlpatterns = [

path('',views.add_model),
path(r'^purchasing/',views.purchasing_view,name="purchasing"),
]

这就是 View 。我在其中定义了两个函数。

bookingform.views

def add_model(request):

if request.method == "POST":
form = Form(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Booking Saved')
# return redirect('/')
return render(request, "template.html", {'form': form})


else:

form = Form()

# purchaing = purchasing.objects.all()

return render(request,"template.html",{'form': form})


def purchasing_view(request):

if request.method == "POST":
purchasing_form = purchasingform(request.POST)
if purchasing_form.is_valid():
purchasing_form.save()
messages.success(request, 'Purchaing Record Saved')
# return redirect('/')
return render(request, "purchasing.html", {'purchasing_form':
purchasing_form})


else:

purchasing_form = purchasing()

# purchaing = purchasing.objects.all()

return render(request, "purchaing.html", {'purchasing_form':
purchasing_form})

最佳答案

假设您想要以下 URL

localhost:8000 -> 映射到 add_model

localhost:8000/purchasing -> 映射到 purchasing_view

对于 projectforms.urls 中的此操作,您需要:

from bookingform.views import add_model, purchasing_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', add_model),
path('purchasing', purchasing_view),
]

关于python - 通过给 django 提供 url 无法打开页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com