Andromedae93 Andromedae93 - 14 days ago 6
Python Question

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


I have an app :
BirthCertificate


My
views.py app
is :

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
:

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 :

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 :

{% 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

I get this error :


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.


Im stuck with Django and I would like to know if you see the error ?

Thank you !

#

EDIT :

I had a mistake in my
forms.py
: I wrote
max_lenght
instead of
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

Answer

Function render() takes template_name as argument. Try to change return in Birth CertificateForm() to this:

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 request.method == 'POST' :
    pass
else:
    pass
Comments