【发布时间】:2021-10-20 15:58:21
【问题描述】:
我希望我的 html 页面在我的用户提交后不再显示该表单,而是显示一个页面,其中包含一条消息,说明该表单已提交。
我想使用注册日期来确定是否提交了表单,但这是我在未提交表单'NoneType' object has no attribute 'registration_date' 时遇到的错误,而如果已经提交了表单,则代码有效。我也不知道是否可以使用缺少注册日期来确定是否提交了表单,我在模型文件中添加了profile_submitted BooleanField 但我无法切换它真实并使用它。
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)
gender = models.CharField(max_length=120, choices=gender_choice)
birthdate = models.DateField()
age = models.CharField(max_length=2)
phone = models.CharField(max_length=10)
email = models.EmailField()
registration_date = models.DateField(default=datetime.today(), blank=True)
profile_submitted = models.BooleanField(default=False)
view.py
def view_profile(request):
profile_is_submitted = UserProfile.objects.filter(user=request.user).first().registration_date is not None
context = {'profile_is_submitted': profile_is_submitted }
return render(request, 'page.html', context)
page.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<h1> Title </h1>
{% if profile_is_submitted %}
You have already submitted the form
{% else %}
<h1> Title </h1>
<div class="container">
<div cass="form-group">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-primary">Post</button>
</div>
</div>
</body>
{% endblock %}
【问题讨论】: