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/94d48bf8-c6ae-4a60-b1af-b9e5a4f8fd40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to