如何在django模板中包含视图功能?

时间:2015-07-14 06:50:46

标签: python django

我是django的新手。所以我需要一个帮助,在Template中包含一个视图功能。我正在寻找,但我很想找到我的期望。我想只使用django模板标签。你能帮帮我吗?

    {View.py

def singUpLunch(请求):     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 for your 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)


 my Template 
{% load url from 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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        New User:&nbsp;&nbsp<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: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="text" name="email" value="{{ email}}" /><br /><br />
            password:
            <input type="password" name="password" value="" /><br /><br />

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<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 i want to call the view singUpLunch function 


{% endif %}

{% endblock %}  

1 个答案:

答案 0 :(得分:0)

您可以看到此Django custom template tags

Django希望模板标签位于名为“templatetags”的文件夹中,该文件夹位于已安装的应用程序中的应用程序模块中...

/my_project/
    /my_app/
        __init__.py
        /templatetags/
            __init__.py
            my_tags.py

#my_tags.py
from 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 a context, which gives us access to the request #}
    <p>Hello, Guest.</p>
{% else %}
    <p>Hello, {{ name }}.</p>
{% endif %}

#main_template.html
{% load my_tags %}
<p>Blah, blah, blah {% say_hello %}</p>

包含标记会像您需要的那样呈现另一个模板,但无需调用视图函数。希望能让你前进。