gpt4 book ai didi

python - Django 找不到页面 404

转载 作者:行者123 更新时间:2023-12-01 03:32:35 25 4
gpt4 key购买 nike

我不明白我的问题出在哪里。我遇到了 url 问题,但我不知道我在哪里犯了错误。

我有一个项目:Etat_civil

我有一个应用程序:BirthCertificate

我的views.py应用程序是:

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from BirthCertificate.forms import BirthCertificateForm

# Create your views here.

def BirthCertificateAccueil(request) :
# Fonction permettant de créer la page d'accueil de la rubrique Acte de Naissance

#Cherche le fichier html accueil et le renvois
template = loader.get_template('accueil.html')
return HttpResponse(template.render(request))


def BirthCertificateCreationAccueil(request) :
# Fonction permettant de créer la page de création du formulaire de la partie : Acte de Naissance

template = loader.get_template('creation_accueil.html')
return HttpResponse(template.render(request))

def BirthCertificateForm(request) :
if request.method == 'POST' :
form = BirthCertificateForm(request.POST)

if form.is_valid():
numero_acte = form.cleaned_data["Numero de l'acte"]
nom = form.cleaned_data['Nom']
prenom = form.cleaned_data['Prenom']

else :
form = BirthCertificateForm()

template = loader.get_template('birthform.html')

return render(request, template.render(request),locals())

我还有一个 urls.py 应用:

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^accueil$', views.BirthCertificateAccueil),
url(r'^creation$', views.BirthCertificateCreationAccueil),
url(r'^formulaire$', views.BirthCertificateForm),
]

我的 urls.py 项目 看起来像:

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

from BirthCertificate import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
]

birthform.html 文件如下所示:

{% if envoi %} Votre formulaire est bien complété ! {% endif %}

<form action="{% url "BirthCertificate.views.BirthCertificateForm" %}" method="post">{%
csrf_token %}
{{ form.as_table }}
<input type ="submit" value="Valider" />
</form>

当我启动时:http://localhost:8000/BirthCertificate/formulaire

我收到此错误:

Page not found (404) Request Method: GET Request

URL: http://localhost:8000/BirthCertificate/formulaire Using the URLconf defined in Etat_civil.urls, Django tried these URL patterns, in this order:

^admin/

^BirthCertificate/ ^accueil$

^BirthCertificate/^creation$

The current URL, BirthCertificate/formulaire, 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.

我一直在使用 Django,我想知道您是否看到了错误?

谢谢!

#

编辑:

我的 forms.py 中有一个错误:我写了 max_lenght 而不是 max_length。但我总是收到错误:

TypeError at /BirthCertificate/formulaire
BirthCertificateForm() missing 1 required positional argument: 'request'
Request Method: GET
Request URL: http://localhost:8000/BirthCertificate/formulaire
Django Version: 1.10.3
Exception Type: TypeError
Exception Value:
BirthCertificateForm() missing 1 required positional argument: 'request'
Exception Location: /Users/valentinjungbluth/Desktop/Django/Etat_civil/BirthCertificate/views.py in BirthCertificateForm, line 34
Python Executable: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3
Python Version: 3.5.2
Python Path:
['/Users/valentinjungbluth/Desktop/Django/Etat_civil',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages']
Server time: Sun, 20 Nov 2016 16:16:16 +0000

最佳答案

函数 render() 将 template_name 作为参数。尝试将 Birth CertificateForm() 中的 return 更改为:

return render(request, 'birthform.html', locals())

编辑:

您可能在这行代码中遇到问题:

form = BirthCertificateForm()

我建议您更改 View 并使用以下模式:

form = BirthCertificateForm(request.POST or None)
if form.is_valid():
numero_acte = form.cleaned_data["Numero de l'acte"]
nom = form.cleaned_data['Nom']
prenom = form.cleaned_data['Prenom']

现在你不需要这个 if-else 验证:

if request.method == 'POST' :
pass
else:
pass

关于python - Django 找不到页面 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40705481/

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