通过提供URL Django无法打开页面

时间:2018-12-15 20:42:15

标签: python django

我无法通过在浏览器上写URL“ localhost:8000 / purchasing”来显示购买模板。我是这里的新手。

  • 项目形式
    • urls.py
  • 预订表格
    • urls.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.urls

from django.urls import path
from . import views


urlpatterns = [

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

这是视图。我已经定义了两个函数。

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})

1 个答案:

答案 0 :(得分:0)

假设您要使用以下网址

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),
]