How to solve 'This field is required' for django forms - django

I created a blog like app. Whenever I update an entry and press the submit button, I want it to take me to view page for that blog entry. However it just redirects me to the update page. When I check form.errors it says that 'This field is required' for all of the fields. But all of the fields are filled with data.
{% extends 'base.html' %}
{% block content %}
{{ form.as_p }}
<form method="POST" action="">
{% csrf_token %}
<input type="submit" value="Update">
</form>
{% endblock content %}

You have your form declared in the wrong block:
{% extends 'base.html' %}
{% block content %}
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock content %}
Notice that the {{form.as_p}} has to be declared into the form block.
If you fill all data but your form is out of the form tag then the data will not be send.

Related

Placement of {% if form.errors %} on login template

I'm not sure if I'm allowed to ask these types of questions here since it's not a problem per say, so please let me know. But I was wondering for the login.html template on Django:
{% extends 'learning_logs/base.html' %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method = "post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name = "submit">log in</button>
<input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
</form>
{% endblock content %}
How come it checks for the form.errors before the it even processes the form? Thanks for any help on this question.
The template ordering doesn't define how the form logic works, only how certain items are displayed. You could put the {% if form.errors %} block after the <form> and there would be no difference to how the form is processed by Django. Furthermore, form.errors will only exist once a form has been validated with is_valid() and any errors exist.

Custom Button in django

I am trying to create custom Button in Django Admin
admin.py
class HeroAdmin(admin.ModelAdmin):
change_list_template="hellogym/hellogymapp/templates/change_list.html"
change_list.html
{% extends 'admin/change_list.html' %}
{% block object-tools %}
<div>
<form action="immortal/" method="POST">
{% csrf_token %}
<button type="submit">Make Immortal</button>
</form>
<form action="mortal/" method="POST">
{% csrf_token %}
<button type="submit">Make Mortal</button>
</form>
</div>
<br />
{{ block.super }}
{% endblock %}
I have created superuser Admin and I want a custom button inside it

Issue assigning form fields in the HTML template django

I have a model form and I wanted to know if it is possible to take a model form and assign the name of the specific field when it is running through a for loop within the template html file.
Here is what i have in the current html template:
{% extends "base.html" %}
{% block content %}
<h1>Add members to {{record.name}}</h1>
{% if message %}
<p>{{message}}</p>
{% endif %}
<form action="." method="POST">
{% csrf_token %}
{% for trans in transactions %}
{% if trans.record.id == record.id %}
{{ trans.user.username }}
{{ form.as_p }}
{% endif %}
{% endfor %}
<p>Tax: <input type="text" name="tax" value=""></p>
<p>Tip: <input type="text" name="tip" value=""></p>
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
here is the current form model:
class IndividualSplitTransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = ['amount', 'description']
currently the format is looking like this:
omar
amount
description
hani
amount
description
assad
amount
description
tax
tip
so when i process it and i want to grab the amount assigned to each of the specific users and update a record. I want it to look something like this for easier processing in the view.
omar
omaramount
omardescription
hani
haniamount
hanidescription
assad
assadamount
assaddescription
tax
tip
so that when i am processing I can properly assign the corrent amount and description for each user when i am updating existing records with the amount.
is it possible to do that within the template itself or would i have to do it somewhere else.
UPDATED
So i tried to change my code and update it to get what i am trying to get but this is what i am getting...
Here is the html
{% extends "base.html" %}
{% block content %}
<h1>Add members to {{record.name}}</h1>
{% if message %}
<p>{{message}}</p>
{% endif %}
<form action="." method="POST">
{% csrf_token %}
{% for trans in transactions %}
{% if trans.record.id == record.id %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ trans.user.username }}{{ field.label }}{{ field|safe }}
</div>
{% endfor %}
{% endif %}
{% endfor %}
<p>Tax: <input type="text" name="tax" value=""></p>
<p>Tip: <input type="text" name="tip" value=""></p>
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
and here is what I am getting.
request returs
So if you look at the image, it is still just returning the last amount and description of the three that i entered... should i just create the whole form manually in the html file....
If you wanted the username + amount or username + description, then you to change away from form.as_p, because you will get the html <p><p/> tag for each field with label preceding it.
You want to dynamically add username then do by designing your own html content of the form manually.
There is a label_suffix, but prefix is not available. Even with suffix you should be doing it at the time of creating the form object.
Update:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ trans.user.username }} {{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}

Django Registration Registration_form

I'm using Django Registration_redux templates for user registrations.
I am wondering how does Django know where to go when user submits his registration when action is action="." ?
{% extends "registration/registration_base.html" %}
{% load i18n %}
{% block title %}{% trans "Register for an account" %}{% endblock %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
{% comment %}
**registration/registration_form.html**
Used to show the form users will fill out to register. By default, has
the following context:
``form``
The registration form. This will be an instance of some subclass
of ``django.forms.Form``; consult `Django's forms documentation
<http://docs.djangoproject.com/en/dev/topics/forms/>`_ for
information on how to display this in a template.
{% endcomment %}
If your action is blank or . then the post goes to the view (URL) which rendered the form.
Here's a simple example using a contact form; https://hellowebapp.com/news/tutorial-setting-up-a-contact-form-with-django/

How do I customize Django's comments form?

I am trying to customize Django's comments form. Inside django.contrib.comments.forms I noticed that all the field forms are declared in the class CommentDetailForm, which is inherited from CommentSecurityForm. Then I think when I write the template tag {% get_comment_form for order as form %}, it's getting the class called CommentForm which inherits CommentDetailForm with a honeypot field.
I wanted to customize the comments form so that it only displays the comments field (and not the optional name, email, or URL fields). Those information will be given by the current logged in user. In fact, only logged in users with certain UserProfile.user_type (UserProfile has a foreign key to User) are allow to comment.
Any tips on how to achieve this? Looking at the source code of the Django's comments already scares me lol.
EDIT:
Here is how the comment template looks so far:
{% get_comment_form for order as form %}
<form action = "{% comment_form_target %}" method = "post">
{% csrf_token %}
{{ form }}
<input type = "submit" name = "submit" value = "Post">
</form>
And the site looks like this
I want to hide Name, Email address, and URL.
You should be able to do all of this in the template:
{% ifequal User.profile.user_type "comment_type" %}
{% get_comment_form for order as form %}
<form action="{% comment_form_target %}" method="post">
{% csrf_token %}
{% for field in form %}
{% ifequal field.name "name" %}
<input id="id_name" type="hidden" name="name" value="{{ user.username }}" />
{% else %}{% ifequal field.name "email" %}
<input type="hidden" name="email" value="{{ user.email }}" id="id_email" />
{% else %}{{ field }}{% endifequal %}{% endifequal %}
{% endfor %}
<input type="submit" name="submit" value="Post">
</form>
{% endifequal %}