Home > Software engineering >  Why am I not getting any feedback as to whether my form is completed correctly or incorrectly? Pytho
Why am I not getting any feedback as to whether my form is completed correctly or incorrectly? Pytho

Time:01-27

I'm working in Django with Python, and here are is my views.py

def register(request):
if request.method == "POST":
    form = NewUserForm(request.POST)
    if form.is_valid():
        user = form.save()
        auth_login(request, user)
        print("success")
        return render(request, 'Upload.html', {})

        # messages.success(request, "Regisration successful.")
        # print("hello")
        return render(request, "Home.html")
    else:
        print(form.errors)
        return render(request, 'Register.html', {'form': form})

form = NewUserForm()
return render(request, 'Register.html', context={"form":form})

This is my HTML

    {% extends 'base.html' %}
    {% load static %}
    {% block css %} 
    <link rel="stylesheet" href=" {% static 'website/Register.css' %} " media="screen">
    {% endblock %}


    {% block content %} 

<section  id="sec-6951">
  <div >
    <div >
      <form action="#" method="POST"  source="custom" name="form" style="padding: 10px;">
        {% csrf_token %}
    

        <div >
          <label for="email-87f0" >Email</label>
          <input type="email" placeholder="Enter a valid email address" id="email-87f0" name="email"  required="">
        </div>
        <div >
          <label for="name-87f0" >Username</label>
          <input type="text" placeholder="Create a username" id="name-87f0" 
           name="username"  required="">
        </div>
        <div >
          <label for="name-12e6" >Password</label>
          <input type="text" placeholder="Create a password" id="name-12e6" name="password1"  required="">
        </div>
        <div >
          <label for="name-12e6" >Verify Password</label>
          <input type="text" placeholder="Verify your password" id="name-12e6" name="password2"  required="">
        </div>
        <div >
          <a href="#" >Create Account</a>
          <input type="submit" value="submit" >
        </div>
       
          <ul >
              {{ messagez }} </li>
              {{ form.errors }}
      
      </form>
    </div>
  </div>
</section>

    {% endblock %}}

The messagez and form.errors is an attempt of getting the code out on the HTML. I have no clue how I can get that code to appear on the HTML. I want it to say "Success" or display the form.error

CodePudding user response:

Consider using alert to display the error message to your intended template.

Perhaps this code in your Template could work.

{% if messages %}
        {% for message in messages %}
        <div  role="alert">
            {{ message }}
        <button type="button"  data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
{% endif %}

For more details, refer to the documentation https://docs.djangoproject.com/en/4.0/ref/contrib/messages/

CodePudding user response:

You'll have a better, more correct time using a FormView rather than implementing a form view as a function-based view.

class RegisterView(FormView):
    form_class = NewUserForm
    template_name = "Register.html"

    def form_valid(self, form):
        user = form.save()
        auth_login(self.request, user)
        return redirect("/")  # assuming that's the Home to redirect to

You'll also have a better time letting Django lay out your form – it will take care of printing errors, initial values, placeholders, etc. too:

<form method="POST">
    {% csrf_token %}
    <table>
    {{ form.as_table }}
    </table>
    <input type="submit" value="submit">
</form>

That default table layout will probably not look like what you want – a popular option to help with that is django-crispy-forms.

  •  Tags:  
  • Related