Why do Django forms display as table? I did define any table as display.
from django import forms
class FieldForm(forms.Form):
field = forms.CharField(label='Field:', max_length=32)
<form action="" method="get">
{{ form }}
<input type="submit" value="Submit" />
</form>
output:
<tr><th><label for="id_field">Field:</label></th><td><input id="id_field" maxlength="32" name="field" type="text" required /></td></tr>
When rendering a form via
{{ form }}
the form's as_table method is called by default (docs, source). Use
{{ form.as_p }}
# or
{{ form.as_ul }}
if you want to render it as paragraphs or an unordered list. If you don't like either of those, you can still manually render fields.
Related
I have custom forms in the page suprated across the page
here is my first form
<form name="profile_image" class="edit-phto" method="POST" enctype="multipart/form-data">
<i class="fa fa-camera-retro"></i>
<label class="fileContainer">
Edit Display Photo
<input name="profile_image" value="" type="file" />
</label>
</form>
for profile picture
and i have another form for cover picture
<form name="cover_image" class="edit-phto" enctype="multipart/form-data">
<i class="fa fa-camera-retro"></i>
<label class="fileContainer">
Edit Cover Photo
<input name="cover_image" type="file" />
</label>
</form>
and another form for data :)
<form method="post" id="infoform" enctype="multipart/form-data">
{% csrf_token %}
.
.
.
.
<div class="submit-btns">
<button type="button" class="mtr-btn"><span>Cancel</span></button>
<button type="submit" class="mtr-btn" name="update"><span> Update</span></button>
</div>
and i have a form for search
<div class="searched">
<form method="post" class="form-search">
<input type="text" placeholder="Search Friend">
<button data-ripple><i class="ti-search"></i></button>
</form>
</div>
i just want to submit the 2 images forms and info forms when i click update button
is there a way to do sachthing in django without using form.as_p and stuff like that
Yes you can, you can just pass request.POST to each of your forms in the view and process each one as normal. However, you should be aware of conflicting names - the best way of managing this is to use form prefixes so that Django knows which bit of data relates to which form:
https://docs.djangoproject.com/en/4.0/ref/forms/api/#prefixes-for-forms
Im on investigation Django framework and now trying to create custom procedure of resetting password.
I use this URL path
path('password_reset_confirm/<uidb64>/<token>',
PasswordResetConfirmView.as_view(
template_name='user_account/reset_password/password_reset_confirm.html',
success_url='/account/password_reset_complete/'),
name="password_reset_confirm"),
In template i use crispy form
<form class="login-form p-4 rounded" method="post">
{% csrf_token %}
<h1 class="h4 mb-4 font-weight-bold">Change your password</h1>
<p>Use the form below to change your password.</p>
{{ form|crispy }}
<button type="submit" value="Change">
Submit
</button>
</form>
So now this form do not showing in template, and if if type {{ form }} instead of {{ form.as_p }} or crispy form, that showing me None in form place.
I am trying to render the name attribute manually.
{% for language in form.languages %}
<div class="form-check">
<input class="form-check-input" id="{{ language.id_for_label }}" name="{{ language.field.name }}" type="checkbox">
<label class="form-check-label" for="{{ language.id_for_label }}">{{ language.choice_label }}</label>
</div>
{% endfor %}
Everything gets rendered nicely except the name attribute of the input tag.
form.languages is a ManyToManyField shown on my form as a ModelMultipleChoiceField using the following code in my forms.py.
languages = forms.ModelMultipleChoiceField(
queryset=Language.objects.all(),
widget=forms.CheckboxSelectMultiple
)
EDIT: Found the culprit, apparently I need a value attribute, not a name attribute, now I just need to find a way to get the value into the template.
I fixed the issue. What I did was use {{ language.data.value }}.
I want to change the classes / attributes of the input fields of a ModelForm, but I'd like to do it from the template if possible.
Below is my solution for this, using the django django-widget-tweaks
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
{{form.title|add_class:"form-control"|attr:"placeholder:Title"|attr:"type:text"}}
</div>
<div class="form-group">
{{form.description|add_class:"form-control"|attr:"placeholder:Description"}}
</div>
<div class="form-group">
{{form.author|add_class:"form-control"|attr:"placeholder:Author"}}
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Submit" />
</div>
</form>
My problem, is that this isn't very DRY, and I'm wondering if there's a better way to do this. I know django has looping over forms, but I'm not sure how I can integrate it while modifying the attributes.
Any help is appreciated.
You can take the placeholder and store it inside the field's help_text field. With that out of the way, you can easily loop through the fields like so:
{% for field in form %}
<div class="form-group">
{{ field|add_class:"form-control"|attr:field.help_text }}
</div>
{% endfor %}
Unfortunately it's hard/impossible to use a filter as for the second argument, so you will need to set help_text to 'placeholder:%s' where %s is your desired value.
If you're using a regular form, then set help_text in the constructor. But if you're using model form, I think it would be best to create a new template tag that can create the 'placeholder:%s' string for you from the field title:
#register.assignment_tag
def get_placeholder_arg(field):
return 'placeholder:%s' % field.label
Then your code would look like this:
{% for field in form %}
{% get_placeholder_arg field as arg %}
<div class="form-group">
{{ field|add_class:"form-control"|attr:arg }}
</div>
{% endfor %}
See the django documentation on custom template tags for details.
I'm trying to create a button on the framework django that let me delete my own article on my blog. I tried to create a code for this functionality, but it doesn't work.
views.py
if(request.GET.get('delete')): #if the button is clicked
delete_article = get_object_or_404(Article, id=id)
if request.POST:
form = DeleteNewForm(request.POST, request.FILES)
if form.is_valid():
delete_article.delete()
return HttpResponseRedirect("/")
template.html
<form enctype='multipart/form-data' action="." method="post" class="form" autocomplete="off" autocorrect="off">
{% csrf_token %}
<div class="form-group">TITRE
{{ form.title.errors }}
{{ form.title }}
</div>
<div class="form-group">CONTENU
{{ form.media }}
{{ form.contenu.errors }}
{{ form.contenu }}
</div>
<div class="form-group">
{{ form.image.errors }}
{{ form.image }}
</div>
<input type="submit" class="btn btn-default" value="Edit Article"/>
<input type="submit" class="btn btn-default" value="Delete Article" name="delete">
</form>
Here is what happen when I submit the form : I am not redirected on the index as it should redirect me and the article has not been deleted.
Is there a problem which don't let me delete my article in these lines?
I have no idea what you're doing in your views or in your template. But if I want to delete something, I just define a separate view for that.
# views.py
from django.views.generic.base import View
class DeleteView(View):
def post(self, request *args, **kwargs):
article = get_object_or_404(id=request.POST['article_id'])
# perform some validation,
# like can this user delete this article or not, etc.
# if validation is successful, delete the article
article.delete()
return HttpResponseRedirect('/')
Your template should be like this:
<form action="/url/to/delete/view/" method="POST">
{% csrf_token %}
<input type="hidden" value="{{ article.id }}">
<input type="submit" value="Delete">
</form>