Django

时间:2016-11-20 14:55:06

标签: python 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请求

     

网址:http://localhost:8000/BirthCertificate/formulaire使用   在Etat_civil.urls中定义的URLconf,Django尝试了这些URL模式,   按此顺序:

     

^ admin /

     

^ BirthCertificate / ^ accueil $

     

^ BirthCertificate / ^ creation $

     

当前网址BirthCertificate / formulaire不匹配   任何这些。您看到此错误,因为您有DEBUG = True   在您的Django设置文件中。将其更改为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

1 个答案:

答案 0 :(得分:2)

函数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