Django页面没有加载表单

时间:2017-03-22 21:09:17

标签: html django

我正在Django开发一个网站。在主页上,它有一个导航栏,我的网站的不同部分列为列表项: enter image description here

如屏幕截图所示,右上方有一个登录按钮。当我点击登录时,一个Modal从顶部掉落,它有一个登录表单。 enter image description here

此安排仅适用于主页。如果我去Navbar中的其他选项卡,我只会得到空模态。 enter image description here

这是我的base.html,其中包含页眉和页脚,并且在我在相应页面中扩展base.html时添加了body部分。

  

{% include "header.html" %}

{% block body %}
{% endblock %}

{% include "footer.html" %}
     

我假设,页面加载如何正确形成,因为在其视图中我通过上下文传递表单变量。现在,由于没有其他页面知道登录表单意味着它只是呈现空模式。在每个页面视图中添加登录表单将过于冗余。有什么方法可以解决这个问题吗?

这是我的登录视图:

def custom_login(req):
    if req.method == 'POST':
        #User Login success
        username = req.POST['username']
        password = req.POST['password']
        user = authenticate (username=username, password=password)
        if user is not None:
            login(req, user)
            return redirect('/login/settings/profile')
        else:
        #User Login Failure
            form = AuthenticationForm()
            args = {'loginForm':form, 'flash':'Invalid Combination of Username and Password'}
            return render(req,'login/login.html', args)
    else:
        if req.user.is_authenticated():
            return redirect('/login/settings/profile')
        else:   
            form = AuthenticationForm()
            args = {'loginForm':form}
            return render(req,'login/login.html',args)

这是我的header.html

{% load crispy_forms_tags %}

<nav class="navbar navbar nav-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="/">DabaKaka</a>
    </div>
    <div id="navbar" class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav">
        <li><a href="">About Us</a></li>
        <li><a href="/packages/">Packages</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        {% if user.is_authenticated %}
          <li class="user-name">{{user.first_name}}</li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"> 
            <span class="glyphicon glyphicon-User"></span><span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="/login/settings/personal"><span class="glyphicon glyphicon-cog"></span> Account Settings</a></li>
              <li><a href="/login/logout"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
            </ul>
          </li>

        {% else %}
          <li><a href="#" data-toggle="modal" data-target="#myModal">Login</a></li>
          <li><a href="/login/register">Register</a></li>
        {% endif %}
      </ul>
    </div>
  </div>
</nav>

  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Enter Username and Password</h4>
    </div>
    <div class="modal-body">
        <form method="post">{% csrf_token %}
      {{ loginForm|crispy }}
      <br>
      {% if flash %}
        <div class="alert alert-danger">{{flash}} </div>
        <br>
      {% endif %}
      <button type="submit" class="btn btn-default">Login</button><br><br>
      <p><small><a href="/login/resetpassword">Forgot Password or Username?</a></small></p>
    </form>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>

  </div>
  </div>

这是主页。

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title %}
    <title>Login</title>
{% endblock %}
     .......Some contents......
{% block body %}
{% endblock %}

1 个答案:

答案 0 :(得分:0)

您可以使用RequestContext和上下文处理器自动在每个模板中添加变量

from django.template import RequestContext, Context
#
#
#

def some_view(request):
    # some view content
    #
    return  render_to_response(template_name,locals(), RequestContext(request,processors=[master_vars]))

def master_vars(request):
    args = user_permissions(request.user)
    # add other vars to args dict
    args["loginForm"] = AuthenticationForm()
    # add more vars as you need
    #
    return args

然后您可以在任何模板中使用这些变量

{{ loginForm|crispy }}