【问题标题】:Django:different html page if form is submittedDjango:如果提交表单,则为不同的 html 页面
【发布时间】: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 %}

【问题讨论】:

    标签: python html django


    【解决方案1】:

    您可以查看.first()的结果,并在模板中相应地传递registration_date的值。

    def view_profile(request):
        profile_is_submitted = False
        userprofile = UserProfile.objects.filter(user=request.user).first()
        if userprofile:
            profile_is_submitted = userprofile.registration_date
    
        context = {'profile_is_submitted': profile_is_submitted }
        return render(request, 'page.html', context)
    

    您不需要这些附加字段,您可以将 userProfile 对象传递到模板中并检查配置文件是否存在,如下所示:-

     def view_profile(request):  
        context = {'user_profile': UserProfile.objects.filter(user=request.user).first() }
        return render(request, 'page.html', context)
    
    
    {% extends 'base.html' %}
    {% block content %}
        <div class="container"> 
            <h1> Title </h1>
    
            {% if user_profile %}
                You have already submitted the form
            {% else %}
                <div cass="form-group">
                    <form method="POST">
                        {% csrf_token %}
                        {{ form.as_p }}
                        <button class="btn btn-primary">Post</button>
                    </form>
                </div>
            {% endif %}
        </div>
    {% endblock %}
    

    【讨论】:

      【解决方案2】:

      您的代码尝试在该 UserProfile 实例上获取“registration_date”,并使用“request.user”(即登录用户)获取该 UserProfile 实例。如果用户没有登录,那将返回一个空的 QuerySet,如果你取第一个元素,它将返回 None。

      您不能在 None 上调用“registration_date”。

      也许检查您是否已登录?或者,如果它是链接到用户的配置文件,请查看如何扩展用户模型:

      https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-30
        • 1970-01-01
        • 2017-02-17
        相关资源
        最近更新 更多