0

So I am using Django version 1.6.5 and I am trying to create a simple form . and I have this view

   def create(request):
       if request.method == 'POST': # If the form has been submitted...
           form = WatchForm(request.POST) # A form bound to the POST data
           if form.is_valid(): # All validation rules pass
               return HttpResponseRedirect('Watch/index.html') # Redirect after POST
           else: 
            return render(request, 'Watch/create.html', {'form': form,})
        else:
            form = WatchForm() # An unbound form

       return render(request, 'Watch/create.html', {
       'form': form,
        })

and this is my form

    from django import forms
    from Watch.models import Watch

    class WatchForm(forms.Form):
        idNumber=forms.CharField(max_length=30)
        brand = forms.CharField(max_length=200)
        #relisedDate =forms.DateTimeField('date published')
        referenceNumber = forms.CharField(max_length=30)
        sortDescription=forms.CharField(max_length=200)
        fullDescription=forms.CharField(max_length=600)

I have also created a template called create.html

    <form action="" method="post">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Submit" />
    </form>

and this is my urlpatterns entry url(r'^create$',views.create),

So when I use this URL /watch/create/ the form does appear in my browser but when I submit I receive this error :

Page not found (404)
Request Method: POST
Request URL:    http://127.0.0.1:8000/watch/create/

   Using the URLconf defined in miWatch.urls, Django tried these URL patterns, in this          order:
   ^watch/ ^$ [name='index']
   ^watch/ ^create$
   ^watch/ ^(?P<watch_id>[0-9]+)/$ [name='detail']
   ^watch/ ^(?P<watch_id>[0-9]+)/results/$ [name='results']
   ^watch/ ^(?P<watch_id>[0-9]+)/vote/$ [name='vote']
   The current URL, watch/create/, didn't match any of these.

and this is the output of the monitor

     [23/Jun/2014 07:53:52] "GET /watch/create HTTP/1.1" 200 1084
     [23/Jun/2014 07:54:01] "POST /watch/create/ HTTP/1.1" 404 2766
     [23/Jun/2014 07:54:08] "GET /watch/create/ HTTP/1.1" 404 2765

Anybody can give me clarification on why is this happening and what is it that I am missing ?? Thanks in advance !

2
  • The error page (when DEBUG=True) should show the list of URLs that were tried. Can you show us that please? Commented Jun 23, 2014 at 12:43
  • On a somewhat related note, please look into using reverse: docs.djangoproject.com/en/dev/topics/http/urls/… Commented Jun 23, 2014 at 13:01

4 Answers 4

2

Change action attribute in your form as

<form action="" method="post">

This will submit the form to current url.

Sign up to request clarification or add additional context in comments.

4 Comments

I did but still getting the same exactly the same error !
Like that ? isn't the second else statement handling the GET method ?
@Nikolis.Py, Have you updated the template? show us updated code
@Nikolis.Py, change your url pattern to has / , like url(r'^create/$',views.create),
2

You're missing a trailing slash:

Using the URLconf defined in miWatch.urls, Django tried these URL patterns, in this          order:
   ^watch/ ^$ [name='index']
   ^watch/ ^create$ #  <-- No Trailing Slash! 

   The current URL, watch/create/, didn't match any of these.

Change your URL by appending a slash:

url(r'^create/$',views.create),

The reason that you are seeing the form display when you're GETting the URL is because you're not adding the slash, and you're loading the page manually. Django likes to have trailing slashes - don't fight that. Not sure why posting to an empty action is appending the slash, but that sounds like a HTML thing rather than a Django issue.

Comments

0

Is your url setting defined on root level ? If so you need to fix the path to

url(r'^watch/create$',views.create)

1 Comment

no it's not it is the one inside the app's directory.
0

Add a slash to your url

url(r'^watch/create/$', views.create)

Return your form re-rendered when its not valid, with data and error messages.

from django.shortcuts import render

def create(request):
    # POST
    if request.method == 'POST': # If the form has been submitted...
        form = WatchForm(request.POST) # A form bound to the POST data
    if form.is_valid(): # All validation rules pass
        # Process the data in form.cleaned_data
        # ...
        return HttpResponseRedirect('Watch/index.html') # Redirect after POST
    else:
        # Not valid, re-render form with data and error messages
        return render(request, 'Watch/create.html', {'form': form,})

    return render(request, 'Watch/create.html', {
      'form': form,
    })

Correct the action in your form template:

<form action="." method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>

1 Comment

I tried what you suggested but it does not seem to solve my issue . When it comes to the url conf file this is the url conf file that is in the specific app not the one that is in the mySite directory ,so I can't add watch/create cause it would look like that watch/watch/create .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.