I create custom django template and used Form (this tag has to be 
universal):

This is file ratings.py use tags:

from django import template
from django.contrib.contenttypes.models import ContentType
from Ratings.forms import RatingsForm

register = template.Library()

class RatingFormNode(template.Node):
    def __init__(self, content, id, varname):
        self.content = content.split('.')
        self.varname = varname
        self.id = template.Variable(id)

    def render(self, context):
        id = self.id.resolve(context)
        content = ContentType.objects.get(app_label=self.content[0], 
model=str(self.content[1]).lower())
        context[self.varname] = RatingsForm(initial={'content_type': 
content, 'object_id': id})
        return ''

@register.tag(name='rating')
def rating(parser, token):
    """
    {% rating appname.Modelname 1 as Newsletter_Form %}
    """
    bits = token.split_contents()

    if bits[3] != 'as':
        raise template.TemplateSyntaxError, "third argument to the 
get_latest tag must be 'as'"

    return RatingFormNode(bits[1], bits[2], bits[4])


and I use this tag in tempalte file:

{% rating appname.Modelname id as rating_form %}
<form action="{% url 'ratings_add' %}" method="POST">
    {% csrf_token %}
    {{ rating_form }}
    <input type="hidden" name="url" value="{{ post.slug }}" />
    <input type="submit" value="Wyƛlij" />
</form>

 
this is urls.py:

from django.conf.urls import patterns, url

urlpatterns = patterns('Ratings.views',
    url(r'^add/$', 'ratings_add', name='ratings_add'),
)

 
and create views to get the url:

# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.http.response import Http404
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from Ratings.forms import RatingsForm


def ratings_add(request):
    if not request.method == 'POST':
        return Http404
    form = RatingsForm(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('post', 
args=[request.POST.get('url')]))
    else:
        request.session['form_data'] = request.POST
        return redirect(reverse('post', args=[request.POST.get('url')]), 
form=form)

 
I use this tag in the template with the url: "/post/name-this-post/". Tag 
creates a form of "RatingForm" of the model "Rating". When the person 
viewing your page and use the form and form is valid this is work (save 
form and redirect to page "/post/name-this-post/"), but when an error 
occurs, you are redirected but does not display an error. How do I fix this?

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to