[英]Page not found 404 with Django
I don't understand where is situated my problem. 我不明白我的问题在哪里。 I get an urls' problem but I don't see where I made a mistake.
我遇到了网址问题,但看不到我在哪里犯了错误。
I have a project : Etat_civil
我有一个项目:
Etat_civil
I have an app : BirthCertificate
我有一个应用程序:
BirthCertificate
My views.py app
is : 我的
views.py app
是:
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())
I have also a urls.py app
: 我也有一个
urls.py app
:
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),
]
And my urls.py project
looks like : 我的
urls.py project
看起来像:
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')),
]
The birthform.html file looks like : 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>
When I launch : http://localhost:8000/BirthCertificate/formulaire 当我启动时: http:// localhost:8000 / BirthCertificate / formulaire
I get this error : 我收到此错误:
Page not found (404) Request Method: GET Request
找不到页面(404)请求方法:GET请求
URL: http://localhost:8000/BirthCertificate/formulaire Using the URLconf defined in Etat_civil.urls, Django tried these URL patterns, in this order:
URL: http:// localhost:8000 / BirthCertificate / formulaire使用Etat_civil.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式:
^admin/
^管理员/
^BirthCertificate/ ^accueil$
^ BirthCertificate / ^ accueil $
^BirthCertificate/^creation$
^出生证明书/ ^ $创建
The current URL, BirthCertificate/formulaire, didn't match any of these.
当前的网址BirthCertificate / formulaire与任何这些都不匹配。 You're seeing this error because you have DEBUG = True in your Django settings file.
您看到此错误是因为Django设置文件中的DEBUG = True。 Change that to False, and Django will display a standard 404 page.
将其更改为False,Django将显示标准的404页面。
Im stuck with Django and I would like to know if you see the error ? 我被Django困住了,我想知道您是否看到错误?
Thank you ! 谢谢 !
EDIT : 编辑:
I had a mistake in my forms.py
: I wrote max_lenght
instead of max_length
. 我的
forms.py
错误:我写了max_lenght
而不是max_length
。 But I get always an error : 但是我总是得到一个错误:
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
Function render() takes template_name as argument. 函数render()以template_name作为参数。 Try to change return in Birth CertificateForm() to this:
尝试将Birth CertificateForm()的返回值更改为此:
return render(request, 'birthform.html', locals())
EDIT: 编辑:
Probably you have problem in this line of code: 这行代码可能有问题:
form = BirthCertificateForm()
I suggest you yo change view and to use following pattern: 我建议您更改视图并使用以下模式:
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']
and now you dont need this if-else validation: 现在您不需要此if-else验证:
if request.method == 'POST' :
pass
else:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.