Why is not there a reusable template for Django's DetailView? - django

Displaying forms in a template is rather easy in Django:
<form action="" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Update" />
</form>
It is basically just one word - display the {{ form }}. It is so simple that you can use the same template for different forms.
You can limit the fields to be shown on the form using the fields = [] list if you are using CBV's such as CreateView or UpdateView.
Drawing parallel to this, one expects to have a similar workflow for showing the models as well (as opposed to editing) such as in DetailView. But, there is no such thing.. You have to write a custom template for every DetailView that you use. Such as:
<h3>User: {{ user }}</h3>
<label>First Name</label>: {{ user.first_name }} <br />
<label>Last Name</label>: {{ user.last_name }} <br />
<label>Username</label>: {{ user.username }} <br />
<label>School</label>: {{ user.person.school.name }} <br />
This is very similar to what the {{ form }} would generate, except for the field values printed here, as opposed toinputs being printed there.
So, I wonder, why isn't there a reusable generic template for DetailView's? Is there a technical limitation for this, or is it just not as reusable as I imagine?

I have created and have been using gladly for about a year now my own generic templates. So, I wanted to share, here it is:
Creating a view is as simple as this:
class PersonDetail(DetailViewParent):
model=Person
DetailViewParent used above (override fields and exclude as needed; default is to include all):
class DetailViewParent(DetailView):
fields=[]
exclude=[]
template_name='common/modal_detail.html'
def get_context_data(self, **kwargs):
context=super(DetailViewParent, self).get_context_data(**kwargs)
context['exclude']=self.exclude
context['fields']=self.fields
return context
Relevant part of the template:
{% fields %}
{% for name, label, value, is_link in fields %}
<tr>
<td><strong>{{ label|capfirst }}</strong></td>
<td>
{% if value.get_absolute_url and request.is_ajax %}
<a class="modal-loader" href="{{ value.get_absolute_url }}">{{ value }}</a>
{% elif value.get_absolute_url %}
{{ value }}
{% else %}
{% if is_link and request.is_ajax %}
<a class="modal-loader" href="{{ value }}">{{ value }}</a>
{% elif is_link %}
{{ value }}
{% else %}
{{ value }}
{% endif %}
{% endif %}
</td>
</tr>
{% endfor %}
And the template tags:
#register.tag(name="fields")
def generate_fields(parser, token):
"""
{% fields %} - loads field name, label, value, is_link to the context
"""
args=token.contents.split()
object_name='object'
if len(args) == 2:
object_name=args[1]
return FieldsNode(object_name)
class FieldsNode(template.Node):
"""
called by generate_fields above
"""
def __init__(self, object_name):
self.object_name=object_name
def render(self, context):
# Get the data necessary for rendering the thing, and add it to the context.
try:
obj=template.Variable(self.object_name).resolve(context)
except template.VariableDoesNotExist:
return ''
include_fields=context.get("fields", None)
exclude_fields=context.get("exclude", None)
fields=[]
for field in obj._meta.fields:
name=field.name
if exclude_fields and name in exclude_fields:
continue
if include_fields and name not in include_fields:
continue
label=field.verbose_name
value=getattr(obj, field.name)
is_link=(type(field).__name__ in ('URLField',))
if isinstance(value, bool):
value=get_bool_check_mark(value)
elif value is None:
value=''
fields.append((
name, label, value, is_link,
))
# If include_fields was defined, then sort by the order.
if include_fields:
fields=sorted(fields, key=lambda field_: include_fields.index(field_[0]))
context['fields']=fields
return ''
The template might be customized to your needs and liking. But I would like to note two things:
1) get_absolute_url: if this (standard django) model method is defined, the field value is shown as url.
2) modal-loader class: this triggers js on the client side to show the detail view in a bootstrap 3 modal. Furthermore, if clicked on a link as mentioned in 1) that is loaded onto the same modal, thus making it easier to browse detail views. It has also a "back" button to go back to the previous model's view. I am not including that here because it is a lot of code, and beyond the scope of this question.

I think it is not as reusable as you imagine.
It might conceivably be possible to define "standard" ways to render simple model properties like CharField - this quickly becomes impossible when you get into more complex relational fields like ManyToManyField, ForeignKey, OneToOneField. You would end up overriding any default representation very quickly for anything but the simplest of models.
Secondly Django is not - and should not be - opinionated about what your models are for, and therefore it makes sense that it doesn't try to assume how you want to render them.
This is different from forms where the structure of individual form fields is defined in Django and in HTML, and there is a strong correlation between the two.

Related

Check for errors and other values at the widget level - maybe using custom form field

How can I access if a field has)errors at the level of widget?
Using default I tried:
{% if widget.attributes.has_errors %} or {% if widget.has_errors %}
but are not working.
I use custom widget templates, I'm thinking to use a custom form Field and overwrite the default field.
I know clean method exist but I don't know how to push to the widget the dynamic(non default) data/attributes I want.
I tried:
class AWidget(forms.Widget):
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
has_errors = context['widget']['attrs'].pop('has_errors', None)
context['widget']['has_errors'] = has_errors
It works for errors but I don't know if is the best option plus I want to pass other values/attributes from Form Field,and I think will be better to try to overwrite the Form Field but I don't know exactly how.
Also accessing individual attributes using:
{{ widget.attrs.maxlength }} or {{ widget.attrs.items.maxlength }}
even if accedes in a for loop works
I know I can add a parent div with a class of error:
<div class="{% if form.field.errors %}pass_error{% endif %}">
{{ form.field }}
</div>
but, that implies big changes at the css level.
I already overwrite all Django widgets with custom widgets, on error I don't need just to change a border color, but to show or not different elements of the widget template and the position of some of them change.
I already modify the based widget to add errors, but I'm looking to do it in a more elegant way at the field level by passing from the field to the widget, parameters depending on error type.
So my question is what I need to overwrite to pass from field to widget errors and other variables ?
Not sure whether this could help in your specific use case ... but just in case, please note that when you build your form in the view, you can add extra parameters as needed, then pass them down to your custom widget.
Working example:
file "forms.py"
from django import forms
def build_ingredient_form(unit):
"""
Ingredient form factory
Here we build the form class dynamically, in order to acces 'unit' via closure.
References:
http://stackoverflow.com/questions/622982/django-passing-custom-form-parameters-to-formset#623030
"""
class IngredientForm(forms.Form):
#quantity = forms.DecimalField(max_digits=10)
quantity = UnitField(unit, required=False)
...
return IngredientForm
file "fields.py"
from django import forms
from .fields import UnitField
class UnitField(forms.CharField):
"""
Custom field to support UnitWidget
References:
- http://tothinkornottothink.com/post/10815277049/django-forms-i-custom-fields-and-widgets-in
"""
def __init__(self, unit, *args, **kwargs):
self.unit = unit
super(UnitField, self).__init__(*args, **kwargs)
self.widget = UnitWidget(unit)
...
file "widgets.py"
from django import forms
from .models import Unit
class UnitWidget(forms.TextInput):
def __init__(self, unit, attrs=None):
if unit is None:
self.unit = Unit()
else:
self.unit = unit
...
Well a widget is how you will render the field's data/value into the HTML rendered template, that's the only function of widgets, look the following example taken from the docs:
>>> name = forms.TextInput(attrs={'required': True})
>>> name.render('name', 'A name')
'<input name="name" type="text" value="A name" required>'
>>>
>>> name = forms.TextInput(attrs={'required': False})
>>> name.render('name', 'A name')
'<input name="name" type="text" value="A name">'
So, widgets are not aware of the data is valid(has errors) or not and should remain that way.
Is not a good idea to handle any data error/validation at the widget level, you want, I can ensure that, that if you change how your field looks like (the widget), your validations keeps working.
Said that ...
How can I access field errors?
When you are rendering a form you can do it field by field lets take this form by example:
class LoginForm(forms.Form):
username = forms.CharField(max_length=255)
password = forms.CharField(widget=forms.PasswordInput)
you can write to temlate:
<form action="." method="get">
<p>{{ loginform.username.label }}: {{ loginform.username }}</p>
<p>{{ loginform.password.label }}: {{ loginform.password}}</p>
<button type="submit">submit</button>
</form>
And this will render something like the following:
Now, suppose your form won't admit passwords with less than 8 characters:
class LoginForm(forms.Form):
username = forms.CharField(max_length=255)
password = forms.CharField(widget=forms.PasswordInput)
def clean_password(self):
password = self.cleaned_data['password']
if len(password) < 8:
raise forms.ValidationError(
"Password must have at least 8 characters, it has only %(password_length)s",
code='invalid password',
params={'password_length': len(password)}
)
return password
You can access the password errors like this:
<form action="." method="get">
{% csrf_token %}
<p>{{ form.username.label }}: {{ form.username }}</p>
<p>{{ form.password.label }}: {{ form.password}}</p>
<ul>
{% for error in form.password.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
<button type="submit">submit</button>
</form>
And now if you type a short password ...
I want the control to look different if there are errors.
You can add some style if there are errors just use {% if ... %} in your template code:
<p>
{{ form.password.label }}:
<span class="{% if form.password.errors %}pass_error{% endif %}">
{{ form.password }}
</span>
</p>
With CSS:
<style>
.pass_error input {
border-color: red;
}
</style>
And this is the result:
Conlusion.
Validate and handle data errors in the form or using validators, use widgets for display the data, of course, you can customize how the data is presented since you can specify a custom template for your widget.
I also recommend django-widget-twaeks if you want to add attributes to your widget in template code. This apps allows you to write code like (example from the app docs):
{% load widget_tweaks %}
<!-- change input type (e.g. to HTML5) -->
{% render_field form.search_query type="search" %}
<!-- add/change several attributes -->
{% render_field form.text rows="20" cols="20" title="Hello, world!" %}
<!-- append to an attribute -->
{% render_field form.title class+="css_class_1 css_class_2" %}
<!-- template variables can be used as attribute values -->
{% render_field form.text placeholder=form.text.label %}

Django template language syntax

I'm learning django and i'm blocked on a template syntax error.
I have this function in my views.py :
def AccountUpdateView(request):
template_name='portal/accountupdate.html'
context = {"forms":UserForm}
return render(request,template_name,context)
There is my template :
<form action="/account/update/" method="POST">
<ul>
{% csrf_token %}
{% for form in forms %}
<li>{{form.label}}
<input type="text" name="{{form.name}}" maxlength="32" required="" id="id_{{form.name}}" value="{{PLEASE HELP ME !!!}}">
</li>
{%endfor%}
</ul>
<input type="submit" value="Metre a jour" />
</form>
Well, i'm trying to get in the "value" of each form on my template by the current registered user known in django by the call {{user}}
And i would to auto place the values of each forms.
I think a solution is to use the form.name (for the example of the case 'username') and in the value call a thing like this :
user.form.username
It doesn't work and i know that i was dream to hope this exotic call don't work...
If any have a solution :)
Thank's you !
You shouldn't do this at all. Django will automatically output the whole field if you ask it.
For a start, use proper names for your objects. Secondly, if you want to prepopulate the form with data from the current user, then do so in the view. Note, you also need to deal with the posted data:
def AccountUpdateView(request):
template_name='portal/accountupdate.html'
if request.method == 'POST':
form = UserForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect('/')
else:
form = UserForm(instance=request.user)
context = {"form":form}
return render(request,template_name,context)
Now, use the proper values and attributes in the template:
{% for field in form %}
<li>{{ field.label_tag }}
{{ field }}
{{ field.errors }}</li>
{% endfor %}

Django 1.11 Form to create multiple instances of model

I am using Django 1.11 for my first Django/Python project and have been stuck for a few days trying to understand Formsets in order to create a form that will allow the creation of multiple model instances through one form. Any assistance that could be provided to help me understand and pass this issue will be greatly appreciated!
The way I have been approaching it is as follows:
I have the following model:
class Task(models.Model):
client_name = models.ForeignKey(Client,null=True,blank=True,on_delete=models.DO_NOTHING)
description = models.CharField(max_length=255)
due_date = models.DateField(null=True,blank=True,default=datetime.now)
assigned = models.ForeignKey(User,on_delete=models.DO_NOTHING)
I have the following form based on the model:
class FNTaskForm(ModelForm):
class Meta:
model = Task
exclude = ()
I have created the folllowing FormSet based on the above model & form:
TaskFormSet = modelformset_factory(Task, form = FNTaskForm, exclude=(),extra=2)
In views.py I have:
class FNTaskCreate(CreateView):
model = Task
form_class = FNTaskForm
template_name = 'fntasks.html'
def get_context_data(self, **kwargs):
context = super(FNTaskCreate, self).get_context_data(**kwargs)
context['formset'] = TaskFormSet(queryset=Task.objects.none()) # providing none
return context
And finally I render it in my html template as:
<form method="post">
{% csrf_token %}
<table class="table link-formset">
{% for form in formset %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
<a class="btn btn-default" href="{% url 'clients' %}">Cancel</a>
<input type="submit" value="Next" class="btn btn-primary" />
</form>
The form appears as follows on the page:
The issue I am having and that which I am gratefully seeking on help on resolving has 2 parts:
Part 1. I cannot successfully save data into the model using this form. I am not getting any errors however I believe that the form has 3 instances, not just the 2 instances displayed and it is a third (not visible instance) that is blank and hence not allowing the form to be valid. The reason I believe this is related to issue 2 (below). When I insert print my kwargs.pop variable to test it I get 3 outputs of 'None'. So I think in my ignorance and inexperience with Django I have not used the model formset factory correctly.
Part 2. I would also like to use kwargs to set the initial value of the 'Client Name' field using the kwarg 'clientlist' passed in the URL. I have used the following to confirm the kwarg but the result is (None None None):
def __init__(self, *args, **kwargs):
clientlist = kwargs.pop('clientlist', None)
super().__init__(*args, **kwargs)
print(clientlist)
The URL is as follows for passing the kwarg:
url(r'^tasks/create/(?P<clientlist>\d+)/$',views.FNTaskCreate.as_view(),name='fntask_create'),
Is anyone able to help me pass these issues to use modelformset_factory (or any other method that is better recommended) to successfuly save multiple model instances with one form with kwargs passed into the initial fields on the form? Thanking you for your time!

Django: Deleting query filters with button in template

I have a listview that displays a list of objects ('Speakers') that has a simple filter on top.
In the template I've put the following code
<form class="col-md-5" method="get">
<div class="input-group">
<select name="type" class="selectpicker" multiple>
{% for type in types %}
<option>{{ type.name }}</option>
{% endfor %}
</select>
<span class="input-group-btn"><button class="btn btn-gray" type="submit">Search</button></span>
</div>
</form>
The types are supplied by the 'get_context_data' method from the listview, like so
def get_context_data(self, **kwargs):
data = super(SpeakerList, self).get_context_data(**kwargs)
typelist = [x for x in Type.objects.all()]
data['types'] = typelist
return data
The filters are handles in the 'get_queryset' method like so
def get_queryset(self, **kwargs):
qs = super(SpeakerList,self).get_queryset().filter(status=STATE_ACTIVE)
types = self.request.GET.getlist('type', None)
categories = self.request.GET.getlist('category', None)
if types:
qs = qs.filter(type__in=types)
if categories:
qs = categories.filter(categories__in=categories)
return qs
So far, so good. However, I want to display all of the selected filters in the template, with a 'remove' button behind them so users can remove specific filters.
The template would then look something like this
[ Type select dropdown ] .
[ Day-speaker 'X'] . [Weekend-speaker 'X']
Object list
Supplying the filters that are used is easy, I've added the following code to the get_context_data
types_query = self.request.GET.getlist('type', None)
data['types_query']
and used the following code in my template:
{% if types_query %}
<p>You used the following 'type' filters:</p>
{% for type in types_query %}
<p>{{ type }} <button>Remove</button> </p>
{% endfor %}
{% endif %}
However, I am not sure how to proceed from here. The list is rendered as it should, but I do not know how to make the 'remove filter' . functionality work.
If I enclose the code above in a form and make it post, it will clear the entire queryset. Yet if I dont the form wont have access to the currently selected filters.
I've seen functionality like this on many websites, so I know it should be possible. Does anyone here know what I am doing wrong?
Anchor with an href the same page with that query parameter removed.
{% for type in types_query %}
<p>{{ type }} Remove </p>
{% endfor %}
You would need to do some processing inside the view to come up with what the URL should look like. This answer can help you there.

django forms - getting django to build the form you want

class BaseForm(forms.Form):
def as_custom_table(self):
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
return self._html_output(
normal_row = u'<tr%(html_class_attr)s><td class="label_col">%(label)s</td><td class="field_col">%(field)s%(help_text)s</td></tr>',
error_row = u'<tr><td colspan="2" class="error">%s</td></tr>',
row_ender = u'</td></tr>',
help_text_html = u'<br />%s',
errors_on_separate_row = True)
I'm trying to see if I can get django to do the heavy lifting and render a form in the way I want it to render.
The problem here is, there might be one or two fields that need to render slightly differently. Such as a "please confirm you agree to our terms and conditions" check box, which would need to span two columns.
Also some other things, such as error placement, might need changing.
I could override the _html_output method to change error placement, but what about getting an individual field to use a different render method?
I think ultimately I need to revert to manually building the form html in the template, but I'm just wondering how much of it Django could do for me with some minor modifications.
The suggested method will be to use a template like this:
<form action="/contact/" method="post">
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>
You can conditionally override specific fields using {{ if field.my_property }}.