Home > Blockchain >  Django:different html page if form is submitted
Django:different html page if form is submitted

Time:10-21

I'd like my html page to not show anymore the form once it is submitted by my user and instead I'd like to show a page with a message that say that the form was already submitted. I thought to use the registration date to determinate whatever the form is submitted or not but this is the error I'm getting when the form is not submitted 'NoneType' object has no attribute 'registration_date' while the code works if there is already a form submitted. I aso don't know if it good to use the presence of the absence of the registration date to determinate if the form is submitted, I've added the profile_submitted BooleanField in my models file but I'm not able to switch it to true and use it.

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 %}

CodePudding user response:

Your code tries to get "registration_date" on that UserProfile instance and it gets that UserProfile instance using "request.user", i.e. the logged in user. If the user isn't logged in, that will return an empty QuerySet and if you take the first element, it'll return None.

You can't call "registration_date" on None.

Maybe check if you are logged in? Or if it's a profile linked to a user check out how to extend the user model:

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

CodePudding user response:

You can check the result of .first() and accordingly pass the value of registration_date in the template.

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)

You dont need these additional fields, you can pass the userProfile object into the template and check if the profile exist or not like below :-

 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 %}
  • Related