Tango with Django chapter 7 Exercises

157 views
Skip to first unread message

Martin Spasov

unread,
Jul 6, 2014, 7:02:32 PM7/6/14
to django...@googlegroups.com

I am doing the exercises in chapter 7 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?

Dean

unread,
Jul 7, 2014, 1:41:26 AM7/7/14
to django...@googlegroups.com
When changing the model, it can help to drop the relevant tables in the db and sync. It kinda flushes things out. That's what I'm finding anyway :-/ I'm only a chapter 5 guru atm, so apologies for my probably unhelpful comments. Maybe you need to restart your server too - have you tried turning it off and on again? :-) Just trying to cure the headache!

Dean

zuchie

unread,
Jul 8, 2014, 7:46:34 AM7/8/14
to django...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages