[英]Page not found 404 with Django
我不明白我的問題在哪里。 我遇到了網址問題,但看不到我在哪里犯了錯誤。
我有一個項目: Etat_civil
我有一個應用程序: BirthCertificate
我的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())
我也有一個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),
]
我的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')),
]
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
我收到此錯誤:
找不到頁面(404)請求方法:GET請求
URL: http:// localhost:8000 / BirthCertificate / formulaire使用Etat_civil.urls中定義的URLconf,Django按以下順序嘗試了這些URL模式:
^管理員/
^ BirthCertificate / ^ accueil $
^出生證明書/ ^ $創建
當前的網址BirthCertificate / formulaire與任何這些都不匹配。 您看到此錯誤是因為Django設置文件中的DEBUG = True。 將其更改為False,Django將顯示標准的404頁面。
我被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 render(request, 'birthform.html', locals())
編輯:
這行代碼可能有問題:
form = BirthCertificateForm()
我建議您更改視圖並使用以下模式:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.