gpt4 book ai didi

javascript - Django 表单不显示

转载 作者:可可西里 更新时间:2023-11-01 13:21:16 25 4
gpt4 key购买 nike

我是 django 表单的新手,在尝试设计 HTML 表单时,我注意到 django 表单 block 没有显示。以下是代码:

<body> index.html 的标签:

<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6">
<a href="#" class="active" id="login-form-link">Login</a>
</div>
<div class="col-xs-6">
<a href="#" id="register-form-link">Register</a>
</div>
</div>
<hr>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
{% block login_content %}
{% endblock %}
{% block register_content %}
{% endblock %}
</div>
</div>
</div>
</div>
</div>
</div>
</div>

login.html进入 {% block login_content %}

{% extends 'index.html' %}
{% block login_content %}
<form id="login-form" action="{% url 'authentication:user_login' %}" role="form" style="display: block;" method="POST">
{% csrf_token %}
<label for="username">Username:</label>
<input id="username" type="text" name="username" placeholder="Enter Username" tabindex="1" class="form-control">

<label for="password">Password:</label>
<input id="password" type="password" name="password" tabindex="=2" class="form-control" placeholder="Password">

<button type="submit" class="btn btn-success">Login</button>

</form>
{% endblock %}

register.html进入 {% block register_content %}

{% extends 'index.html' %}
{% block register_content %}
<form id="register-form" role="form" style="display: none;" enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit" name="" value="Register">
</form>
{% endblock %}

在我的用户界面上,{{ user_form.as_p }}{{ profile_form.as_p }}根本没有出现。

当我检查我的 UI 控制台时,html 甚至没有从 register.html 中获取以下行:

<form id="register-form" role="form" style="display: none;" enctype="multipart/form-data" method="POST">

这是 login.js负责 UI 交互性:

$(function() {

$('#login-form-link').click(function(e) {
$("#login-form").delay(100).fadeIn(100);
$("#register-form").fadeOut(100);
$('#register-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
});
$('#register-form-link').click(function(e) {
$("#register-form").delay(100).fadeIn(100);
$("#login-form").fadeOut(100);
$('#login-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
});

});

编辑:这是我的 views.py :

def user_login(request):
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')

user = authenticate(username=username, password=password)

if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('homepage')) # main page if login is successful

else:
return HttpResponse("ACCOUNT NOT ACTIVE")

else:
print('Someone tried to log in and failed')
print('Username: {}, password, {}'.format(username, password))
return HttpResponse('Invalid login details supplied')

else:
return render(request, 'authentication/login.html', {})

def register(request):
registered = False

if request.method == "POST":
user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(data=request.POST)

if user_form.is_valid() and profile_form.is_valid():

user = user_form.save()
user.set_password(user.password)
user.save()

profile = profile_form.save(commit=False)
profile.user = user

if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']

profile.save()

registered = True

else:
print(user_form.errors, profile_form.errors)

else:
user_form = UserForm()
profile_form = UserProfileInfoForm()

return render(request, 'authentication/register.html',
{
'user_form': user_form,
'profile_form': profile_form,
'registered': registered
})

好吧,看起来不可能让一个页面显示两个模板。有什么方法可以完成吗?

最佳答案

在你的register.html中,

<form id="register-form" role="form" style="display: none;" enctype="multipart/form-data" method="POST">

您正在设置 style="display: none;",所以显然表单不会显示。

编辑:我不确定在同一页面中显示两种不同形式的可行性。

关于javascript - Django 表单不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com