表单字段未显示在html页面django上

时间:2018-07-31 23:01:22

标签: html django django-forms django-templates mdbootstrap

这是我在登录页面中使用的模板,但问题是字段未显示。我想在页面上使用mdbootstrap。我已经在许多网站上进行了搜索,但没有解决方案,我在代码中缺少的东西是使用同一东西使用表单吗?

<form action="{% url 'log_in' %}" method="POST">

              {% csrf_token %}

    <div class="md-form">

        <i class="fa fa-envelope prefix"></i>
        {{ form.username }}
        {{ form.username.label_tag }}

     </div>

    <div style="padding:5px"></div>

    <div class="md-form"  >

        <i class="fa fa-lock prefix"></i>
        {{ form.password.label_tag }}
        {{ form.password }}

    </div>
            {% if requset.GET.next %}
            <input type="hidden" name="next" value="{{ request.GET.next }}">
            {% endif %}

    <button type='submit' class="btn info-color ">log in</button>

 </form>

和forms.py

from django.contrib.auth.forms import AuthenticationForm
from django import forms


class LoginForm(AuthenticationForm):
    username = forms.CharField(label="Username", max_length=30,
                               widget=forms.TextInput(attrs={'class': 'form-control', 'name': 'username'}))
    password = forms.CharField(label="Password", max_length=30,
                                widget=forms.PasswordInput(attrs={'class': 'form-control', 'name': 'password'}))

我的查看功能是

def log_in(request):
    if request.user.is_authenticated:
        return render(request,'registration/userhome.html')
    elif request.POST:
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user.is_active:
            login(request, user)
            return render(request,"registration/userhome.html")
    else :
        return HttpResponse("Invalid login details supplied.")        

查看login_view()

def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = LoginForm()
    return render(request,"registration/login.html",{'form':form})

我什至没有呈现登录页面,但是它们仍然可以正常工作,并且表单字段的一部分丢失了

我的urls.py文件

from django.urls import path
from . import views
from django.contrib.auth.views import LoginView


urlpatterns =[

    path("",views.index,name='index'),
    path("userhome/",views.userhome,name='userhome'),
    path("quiz/", views.quiz, name='quiz'),
    path("signup/", views.sign_up,name='sign_up'),
    path("login/", views.login_view,  name='login'),
    path("login/", views.log_in,  name='log_in'),
    path("index", views.log_out,name='log_out'),
    path("rules/",views.rules,name='rules'),

]

1 个答案:

答案 0 :(得分:0)

您必须具有这样的视图:

def myView(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = LoginForm()
    return render(request, 'myTemplate.html', {'form': form})