Django - 在同一页面上有一个基于类的视图和Crispy表单 [英] Django - Have a class based view and Crispy form parrallell to each other on same page
问题描述
使用Django,我正在制作一个显示票据的网站。在登录页面上,我希望它位于登录表单左侧现有票证的预览区域。我能够单独制作两页,但我无法弄清楚如何使它们并排共存。我也在使用jinja2模板。我一直在尝试2天,而且我一遍又一遍地阅读了教程。这是我的方式:
url.py:
urlpatterns = patterns('',
url(r'',include('ticket_app.urls')),
url(r'^ accounts / login / $',login,{'authentication_form': forms.LoginForm},name ='login'),
)
views.py注意:Request是数据库表对象:
$ p $ class PreviewList(ListView):
model =请求
form.py:
class LoginForm(AuthenticationForm):
def __init __(self,* args,** kwargs):
super(LoginForm,self).__ init __(* args,** kwargs)
self .helper = FormHelper()
self.helper.form_class ='form-horizontal'
self.helper.label_class ='col-lg-2'
self.helper.field_class ='col -lg-8'
self.helper.form_tag = False
self.helper.layout =布局(
字段('username',placeholder =用户名,css_class ='input-x (''''',''''),
字段('密码',占位符=密码,css_class ='input-xlarge'),
FormActions(
提交('login','Login',css_class =btn-primary),
)
base.html(简短版本):
< h1> {%block page_title%}预览门票{%endblock page_title%}< / h1>
{%block preview%}
{%endblock preview%}
{%block login%}
{%endblock login%}
preview.html:
{%extends'base.html'%}
{%block preview%}
< div class =span6>
< ul class =list-group>
{%if object_list%}
{%为object_list%中的项目}
< span class =badge>
{%if item.user_assigned%}
< span class =badgestyle =color:green>分配给< / span>
{%else%}< span class =badgestyle =color:red>未分配< / span>
{%endif%}
< / li>
{%endfor%}
{%else%}
Yay!找不到机票要求!< / p>
{%endif%}
< / div>
{%endblock preview%}
login.html:
{%extends'base.html'%}
{%load crispy_forms_tags%}
{ %block login%}
< div class =containerstyle =padding-bottom:70px;>
< div class ='row'>
< div class ='col-md-6 col-md-offset-3'>
< div class =well>
<图例>登入网站< / legend>
< form method =postaction ={%url'django.contrib.auth.views.login'%}class =form-horizontal>
{%crispy form%}
< input type =hiddenname =nextvalue ={{next}}/>
< / form>
< / div>
< / div>
< / div>
< / div>
{%endblock login%}
有多种方法可以实现这一点。例如,您可以创建您自己的登录视图,用于扩展来自 django.contrib.auth
的一个视图:
<$来自django.contrib.auth.views的p $ p>
导入登录
def login_with_preview(request,extra_context = None,* args,** kwargs):
if extra_context is None:
extra_context = {}
extra_context ['object_list'] = Request.objects.all()
返回登录(request,authentication_form = LoginForm,extra_context = extra_context,* args, ** kwargs)
然后使用 login_with_preview
在你的url conf中并合并你的两个模板。
另一种可能性是创建一个自定义模板标签。如果你想在不同的地方展示这个预览列表,这个特别好:
@ register.inclusion_tag(yourapp / preview_list。 html)
def ticket_preview_list():
return {'object_list':Request.objects.all()}
preview_list.html
仅应 包含目前位于预览
模板块。您需要确保模板不会扩展 base.html
;这会搞砸了。通常使用模板标记设置步骤。
Using Django, I am making a site that displays tickets. On the login page, I want it to where there is a preview area of existing tickets to the left of the login form. I am able to make both pages seperately, but I can not figure out how to make them co-exist side by side. I am also using jinja2 templating. I've been trying at this for 2 days, and I have read the tutorials over and over. Here is how I have it:
url.py:
urlpatterns = patterns('',
url(r'', include('ticket_app.urls')),
url(r'^accounts/login/$', login, {'authentication_form': forms.LoginForm}, name='login'),
)
views.py Note: Request is the database table object:
class PreviewList(ListView):
model = Request
form.py:
class LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-2'
self.helper.field_class = 'col-lg-8'
self.helper.form_tag = False
self.helper.layout = Layout(
Field('username', placeholder="Username", css_class='input-xlarge'),
Field('password', placeholder="Password", css_class='input-xlarge'),
FormActions(
Submit('login', 'Login', css_class="btn-primary"),
)
base.html(short version):
<h1>{% block page_title %}Preview of tickets{% endblock page_title %}</h1>
{% block preview %}
{% endblock preview %}
{% block login %}
{% endblock login %}
preview.html:
{% extends 'base.html'%}
{% block preview %}
<div class="span6">
<ul class="list-group">
{% if object_list %}
{% for item in object_list %}
<li class="list-group-item">{{item.date_due}} - {{item.desctription}}
<span class="badge">
{% if item.user_assigned %}
<span class="badge"style="color:green"> assigned </span>
{% else %}<span class="badge" style="color:red">unassigned</span>
{% endif %}
</li>
{% endfor %}
{% else %}
<p>Yay! No ticket requests found!</p>
{% endif %}
</div>
{% endblock preview %}
login.html:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block login %}
<div class="container" style="padding-bottom: 70px;">
<div class='row'>
<div class='col-md-6 col-md-offset-3'>
<div class="well">
<legend>Sign in to Site</legend>
<form method="post" action="{% url 'django.contrib.auth.views.login' %}" class="form-horizontal">
{% crispy form %}
<input type="hidden" name="next" value="{{ next }}"/>
</form>
</div>
</div>
</div>
</div>
{% endblock login %}
There are multiple ways to accomplish this. For example, you could create your own login view that 'extends' the one from django.contrib.auth
:
from django.contrib.auth.views import login
def login_with_preview(request, extra_context=None, *args, **kwargs):
if extra_context is None:
extra_context = {}
extra_context['object_list'] = Request.objects.all()
return login(request, authentication_form=LoginForm, extra_context=extra_context, *args, **kwargs)
And then use login_with_preview
in your url conf and merge your two templates.
Another possibility is to create a custom template tag. This is especially nice if you want to show this preview list in different places:
@register.inclusion_tag("yourapp/preview_list.html")
def ticket_preview_list():
return {'object_list': Request.objects.all()}
preview_list.html
should only contain the template code that, right now, is in your preview
template block. You need to make sure that template doesn't extend base.html
; that would mess things up. The usual template tag setup steps apply.
这篇关于Django - 在同一页面上有一个基于类的视图和Crispy表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!