How do I update a single field in django using html template?
I have an html page to add records. I need to make an html page only to update a single field of the model. How do I do that. And what should be the view function for it.
It's really quite simple with an UpdateView.
It takes care of building a generic ModelForm class for the model too.
class UpdateSomeFieldView(views.UpdateView):
fields = ('some_field',)
model = MyModel
For the template, all you really need there is a
<form method="post">
{% csrf_token %}
{{ form.errors }}
<table>{{ form.as_table }}</table>
<input type="submit" />
</form>
Related
I am trying to implement a simple UpdateView, but I want to use my own template. Using djangos auto_population is working, but not what I want, because I have lots of fields formatted differently.
<form method="post" action="#">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
But I want to use my own form template which looks like this:
edit_template.html
<form class="form-horizontal" role="form" method="POST" action="{% url 'update' pk=abc %}">
{% csrf_token %}
<input name='varX' id="varX" type="text" placeholder="" class="form-class">
<input name='varY' id="varY" type="text" placeholder="" class="form-class">
</form>
views.py
class ModelUpdate(UpdateView):
model = MyModel
fields = ['varX','varY']
Now I would like that form to be populated with my object data, but the form is empty.
UpdateView is also passing the data twice to the template: One as 'object' and one as 'mymodel'.
I also tried updating
get_context_data
by adding
context.update( model_to_dict(myModelData))
But that also does not change anything.
How can I populate my custom form using djangos class-based views?
Try this:
def get_initial(self):
initial = super().get_initial()
initial['my_form_field1'] = self.request.something
return initial
Have a look at django-widget-tweaks, you first need to check for any non field errors and then loopt through the fields one by one.
https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html
This article should cover just that
I have generated a simple contact form with four fields using Wagtail Form Builder. The challenge I'm having, is that I'm unable to reference the form fields with css classes individually, since I'm not sure where I should get the field id and label name from.
Here is the code that renders the form:
<h1>{{ page.title }}</h1>
{{ page.intro|richtext }}
<form action="{% pageurl page %}" method="POST" class="sky-form contact-style">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
If referencing the fields individually is not possible, what will be the other method(s) to achieve the same result?
Wagtail formbuilder creates a dynamic form - the id (name) of each field is created by the following code: str(slugify(text_type(unidecode(self.label))))
(from https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailforms/models.py#L90)
So the field label is converted to ascii characters and then to a slug. For example, if you have a field name First Name it will be converted to 'first-name'.
An easy way to find out what is the actual id of the form fields that wagtail formbuilder creates is to actually output the fields in a template by enumerating them:
{% for field in form %}
{{ field }}
{% endfor %}
and then inspect the produced html code to see their actual ids (you'll see <input class="textinput textInput" id="id_first-name" maxlength="255" name="first-name" type="text"> so you'll know that the id of the field is first-name)
I'd like to add a single form input field to a DetailView that will append tags to my model. I'm not sure if I need a separate view for that, or I can simply add a form. Using django-bootstrap3, I have the following:
<div class="row">
<form action="add-tag" method="post"
enctype=multipart/form-data>
{% csrf_token %}
{{ form.media }}
{% bootstrap_field form.tags show_label=False %}
<input type="submit" value="{% trans "submit" %}"/>
</form>
</div>
forms.py:
class MyModelAddTagsForm(forms.ModelForm):
class Meta:
model = models.MyModel
fields = ('tags',)
I get the error:
BootstrapError at /someurl/1/view
Parameter "field" should contain a valid Django BoundField.
I'm not sure what else I exactly need. A view and url or modifying my DetailView? Or using an UpdateView instead?
I am using django-webtest to create automated functional tests for my application.
As I have multiple forms on a single web page, I'm currently using a hard-coded index to select the form of interest. For instance:
# Forms on this page are: forms[0] = Establishment form,
# forms[1]= School Form
school_form = school_page.forms[1]
school_form['phone'] = self.school_phone_number
school_form.submit(value='Submit')
I would like to use the form id instead of a hard-coded index to select the form of interest, so that the code is maintainable. But when I print school_form.id in my code, the value is None.
Template is:
<form action="" method="post" >
{% csrf_token %}
<p>
{{ establishment_form|crispy }}
<br><br>
{{ school_form|crispy }}
</p>
<button type="submit" class="btn btn-lg btn-primary">Submit</button>
I couldn't locate in the Django documentation how to assign the form id (not the form field id) to a ModelForm. Could someone help me with this?
I'm using Django 1.7, django-webtest 1.7.8, WebTest 2.0.18 and django-crispy-forms 1.4.0.
Django forms aren't responsible for outputting the form element itself. So there is nothing to be done at the form declaration level.
You can always pass an attribute from the view and use it in your template:
<form id="{{ form_id_from_view }}" action="." method="POST">
You can directly assign the id to the form element in your template.
<form id="my_id" name="some_name" action="/my/url/" method="POST">
I am working in Django 1.5. I have a task submitting a form and editting it. My form contains order details, customer details, purchased product details, attachments and comments. Order details model have customer foreign key, product details have order foreign key. I am using modelform objects of order details,customer details,attachments and comments and inlineformset_factory object for products. I have successfully inserted data using this 'multi-object' form. Now I would like to edit this form. All these form objects have to be passed to the template. Please help me. Thanks in advance.
With an existing template it's very easy to create your forms. Just add your specific forms to your template like this example below:
<form action="{% url 'changeprofile' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ user_form }}
{{ location_form }}
{{ user_profile_form }}
<input type="submit" value="Submit" />
</form>
Of course your view needs to instantiate them correctly.
from django import shortcuts
...
user_form = UserForm
user_profile_form = UserProfileForm2(initial=inital_form_values)
location_form = LocationForm
forms = {'user_form':user_form,
'user_profile_form':user_profile_form,
'location_form':location_form}
return shortcuts.render(request, 'changeprofile.html', forms)
The enctype="multipart/form-data" you only need if you have data like files or images.