python - How can i include a view function in django template? -
i new in django. need including view function within template. searching tired finding expectation. want use django template tag. please me?
{view.py
def singuplunch(request): query_results = menu.objects.all()
form=singupform(request.post or none) if form.is_valid(): save_it=form.save(commit=false) save_it.save() messages.success(request,'thanks co-operation') return render_to_response('singup.html',locals(),context_instance=requestcontext(request))
}
{my model class singup(models.model): employee_id = models.autofield(unique=true,primary_key=true) name = models.charfield(max_length=20,choices=status_choices) email = models.emailfield() date = models.datefield() lunch = models.booleanfield() class meta: unique_together = ["name", "email","date"] ordering = ['-date'] username_field = 'employee_id' required_fields = ['mobile'] def __unicode__(self): return smart_unicode(self.email) } class singup(models.model): employee_id = models.autofield(unique=true,primary_key=true) name = models.charfield(max_length=20,choices=status_choices) email = models.emailfield() date = models.datefield() # auto_now_add=true, blank=true default=date.today(),blank=true lunch = models.booleanfield() class meta: unique_together = ["name", "email","date"] ordering = ['-date'] username_field = 'employee_id' required_fields = ['mobile'] def __unicode__(self): return smart_unicode(self.email) template {% load url future %} {% block content %} {% if not email %} <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>log in</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style> body { margin: 0; padding: 0; color: #555; font: normal 12pt arial,helvetica,sans-serif; background:#ffffff url(check2.jpg) repeat-x; width: 130%; height: 100%; position: fixed; top: 40px; left: 480px } .span3.well { min-height: 20px; padding: 19px; margin-bottom: 20px; background-color: #f5f5ff; border: 1px solid #e3e3e3; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); } </style> </head> <body> <head><div class='page-header pagination-centered'><img src=/static/logo.png class='img-rounded pagination-centere' /></div> </head> {{ state }} <div class="span3 well" style="height: auto;margin-left: 1.7%;width: 30%;"> <h2>please login new user:  <a href="/signup/"> signup</a></h2> <div class="leftpane"> <form action="" method="post"> {% csrf_token %} {% if next %} <input type="hidden" name="next" value="{{ next }}" /> {% endif %} email: <input type="text" name="email" value="{{ email}}" /><br /><br /> password: <input type="password" name="password" value="" /><br /><br /> <input type="submit" value="log in" /> </form> </div> <div class="aboutus_portion"> </div> </div> <!-- <div id="rightcontent"> <li class=" dir"><a href="/accounts/login/"><h3>admin</h3></a> </li> </div> --> </body> </html> {% else %} {% include 'singuplunch' %} # here want call view singuplunch function {% endif %} {% endblock %}
you can see django custom template tags
django expects template tags live in folder called 'templatetags' in app module in installed apps...
/my_project/ /my_app/ __init__.py /templatetags/ __init__.py my_tags.py #my_tags.py django import template register = template.library() @register.inclusion_tag('other_template.html') def say_hello(takes_context=true): return {'name' : 'john'} #other_template.html {% if request.user.is_anonymous %} {# our inclusion tag accepts context, gives access request #} <p>hello, guest.</p> {% else %} <p>hello, {{ name }}.</p> {% endif %} #main_template.html {% load my_tags %} <p>blah, blah, blah {% say_hello %}</p>
the inclusion tag renders template, need, without having call view function. hope gets going.
Comments
Post a Comment