try in step 2:

action = ""

leave it blank, the form will be posted in the same view.

and in step 4:

<a href="/blog/category/{{ category.url }}/add_page/"> Add Page</a>

of course you have to insert url into the context dic this way in 
views/category(): category.url = category_name_url.
I think your way the url will be processed as category//add_page/, the page 
might not be found.

try the changes above and let me know if that works for you.

On Sunday, July 6, 2014 12:02:32 PM UTC-7, Martin Spasov wrote:
>
> I am doing the exercises in chapter 7 
> <http://www.tangowithdjango.com/book/chapters/forms.html#exercises> and I 
> have to create an add page link on every category page that would take the 
> users to a new page on which they can enter name and url to add to certain 
> category if the category in question is not existing they would be 
> redirected to the add category page.
>
> so the steps are:
>
> 1.Create a new view (the tut gives us this one ready)
>
> def add_page(request, category_name_url):
>     context = RequestContext(request)
>     category_name = category_name_url.replace('_',' ')
>     if request.method == 'POST':
>         form = PageForm(request.POST)
>
>         if form.is_valid():
>             page = form.save(commit = False)
>
>             try:
>                 cat = Category.objects.get(name = category_name)
>                 page.category = cat
>             except Category.DoesNotExist:
>                 return render_to_response('blog/add_category.html', {} , 
> context)
>
>             page.views = 0
>             page.save()
>
>             return category(request, category_name_url)
>         else:
>             print form.errors
>
>    else:
>         form = PageForm()
>
>     return render_to_response('blog/add_page.html', {'category_name_url': 
> category_name_url, 'category_name': category_name, 'form': form}, context)
>
> 2.create a new template
>
> <!DOCTYPE html>
> <html>
>     <head>
>         <title> Drib </title>
>     </head>
>
>     <body>
>         <h1>Add Page</h1>
>
>         <form id = 'page_form' method = 'post' action = '/blog/add_page/'>
>             {% csrf_token %}
>             {%for hidden in forms.hidden_fields%}
>                 {{hidden}}
>             {%for field in forms.visible_fields%}
>                 {{field}}
>                 {{field.errors}}
>                 {{field.help_text}}
>             <input type = 'submit' name='Submit' value = "Add Page" />
>         </form>
>     </body>
>
> </html>
>
> 3.URL Mapping
>
> url(r'^category/(?P<category_name_url>\w+)/add_page/$', views.add_page , name 
> = 'add_page')
>
> 4.Create a link from the category page (I am just going to post the link 
> because there is no point to post the whole file...
>
> <a href="/blog/category/{{category_name_url}}/add_page/"> Add Page</a>
>
> Hints from the tut are :
>
> -Update the category() view to pass category_name_url by inserting it to 
> the view’s context_dict dictionary - DONE
>
> -Update the category.html with a link to /rango/category//add_page/. 
> Ensure that the link only appears when the requested category exists - with 
> or without pages. - I believe i did that in step 4 (not 100 % sure though)
>
> -Update rango/urls.py with a URL mapping to handle the above link.(Step 3 
> i believe)
>
>
> My question is why when clicking on add page and entering the details 
> needed it doesnt add the page at all its not even doing the is_valid() i 
> believe because the same function works perfectly when creating 
> categories(i get a message if the cat is already created or if i leave it 
> blank) but here it just redirects me and thats all?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/64541164-831b-4aa4-bdb9-3282f54be6a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to