Django不会在多个模板中呈现表单吗?

时间:2019-07-03 16:35:54

标签: python django django-crispy-forms

Django的新手。酥脆的表格在注册页面上呈现精美。精确复制和粘贴不会在登录页面上呈现该表单吗?感谢所有帮助。

我也尝试过从方程式中删除Crispy,但是没有运气。

main_project / urls.py

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^\/eureka\/services-details\.php
RewriteCond %{QUERY_STRING} id=34
RewriteRule ^(.*)$ https://www.eurekahomehealthcare.com/service/babysitting-services/? [L,R=301]

users / forms.py

let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)

users / views.py

from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from  users import views as user_views
from website import views as website_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', user_views.register, name='register'), 
    path('home/', website_views.home, name='site-home'),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout')
]

users / register.html AND users / login.html

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class UserRegisterForm(UserCreationForm):
    first_name = forms.CharField(max_length=100)
    last_name = forms.CharField(max_length=100)
    email = forms.EmailField()
    address = forms.CharField(max_length=100)

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

1 个答案:

答案 0 :(得分:0)

@Daniel Roseman完美解决了这个问题,请参见评论。

基本上,以上情况有点混乱。 Django类ListView()已经向您选择的模板返回了一个表单(这很重要)。使用form而不是register_form作为form标签可以使一切正常运行。请参阅下面的更正代码。

users/login.html

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

<!-- Extra Styling? -->
{% block extrahead %}
{% endblock extrahead %}

<!-- Include Navbar? -->
{% block navbar %}
{% include 'navbar.html' %}
{% endblock navbar %}

<!-- Main HTML Page Cotent -->
{% block content %}

<div class="container">
        <form>
            <fieldset>
                <div class="row justify-content-center">
                    <div class="col-6">
                        {% csrf_token %}
                        <legend>
                            {{form|crispy}}
                        </legend>
                    </div>
                </div>
            </fieldset>
            <div class='row justify-content-center'>
                <div class="col-6">
                    <button type="submit" class="btn btn-success float-right">Register</button>
                </div>
            </div>
        </form>
    </div>

{% endblock content %}