I create custom django template and used Form (this tag has to be universal):
This is file ratings.py use tags:
from django import templatefrom django.contrib.contenttypes.models import ContentTypefrom Ratings.forms import RatingsFormregister = template.Library()class RatingFormNode(template.Node):def __init__(self, content, id, varname):self.content = content.split('.')self.varname = varnameself.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, urlurlpatterns = 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 reversefrom django.http.response import Http404from django.http import HttpResponseRedirectfrom django.shortcuts import redirectfrom Ratings.forms import RatingsFormdef ratings_add(request):if not request.method == 'POST':return Http404form = RatingsForm(request.POST)if form.is_valid():form.save()return HttpResponseRedirect(reverse('post', args=[request.POST.get('url')]))else:request.session['form_data'] = request.POSTreturn 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 django-users+unsubscribe@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.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment