How do I get the latest post in a topic? - python

In the table, I want to show the latest post from that topic. My post model is called post. I have listed my code below.
forum.html:
{% for category in categories %}
<h5 class="blue-text">{{ category.title }}</h5>
<table>
<thead>
<tr>
<th>Icon</th>
<th>Name</th>
<th>Latest Post</th>
</tr>
</thead>
{% for topic in category.topic_set.all %}
<tbody>
<tr>
<td><i class="material-icons black-text">{{ topic.icon }}</i></td>
<td>{{ topic.title }} <br> <span class="grey-text">{{ topic.description }}</span></td>
<td>post_title <br> <span class="grey-text">post_author</span> <br> <span class="grey-text">post_created_at_timesince</span></td>
</tr>
{% empty %}
<p>No topics in {{ category.title }}</p>
{% endfor %}
</tbody>
</table>
{% empty %}
<p>No categories.</p>
{% endfor %}
views.py:
def forums(request):
categories = Category.objects.all()
topics = Topic.objects.all()
return render(request, 'forum.html', {'categories': categories, 'topics': topics})
If you need anymore details let me know.
Thanks,
Cameron.

use Topic.objects.order_by('-createtime') to order them by createtime. If you don't have createtime in your field, you can use -id instead since the lastest topic has the largest id.

To get the latest topic, you would do something like,
latest_topic = Topic.objects.order_by('-createtime')[-1]
or
latest_topic = Topic.objects.order_by('-id')[-1]

This can be done in following way. You can change your view like this
from django.db.models import Prefetch
def forums(request):
topics = Topic.objects.all().order_by('-created_time')
categories = Category.objects.all().prefetch_related(Prefetch('topic_set', queryset=topics)
return render(request, 'forum.html', {'categories': categories})
Above code snippet has two advantages:
You will get latest post when you do {% for topic in category.topic_set.all %}.
No extra query will execute for the above statement in the template.
You can add topics in the context if you are using at some other place also. For the given case, you do not need to send it in the context since you are getting the data from the category object.
If you require that you need to get latest post of category at multiple places, then you can also use ordering attribute in the meta class of the topics model.
You can find more details here https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering.

Related

Django Search And View more

I have search results in form of table. I want to add a detail page button which can send the id of the result to another function in view.py
so i can query it from database.
{% if sr %}
{% for k,j in sr %}
<tbody>
<tr>
<td>{{ k.id }}</td>
<td>{{ k.chromosome }}</td>
<td>{{ k.gene_id }} </td>
<td> view</td>
</tr>
</tbody>
{% endfor %}
{% endif %}
I want to send this k.id to another function
def detailed(request):
return render(request,"search/Detailed.html")
so I can again perform a query from database by this id
Since you didn't specify the version of django you're using, I'm going to assume it's 2.x. The only huge difference between that and more recent versions of 1.x is the urlpatterns. If you're using 1.11, just use the required regex's you need, as described in the docs. Either way, the principle is the same.
urls.py
urlpatterns = [
....
path('<int:some_id>/', views.detail_view, name='detail_view'),
# if django 1.11... you would use ([0-9]{4}) in place of the 'int', or for whatever max amount of numbers you'd want to capture... But I will continue for django 2.x.
]
views.py
def detail_view(request, some_id):
some_object = YourModel.objects.get(id=some_id)
return render(request, 'detail_template.html', {'some_object ': some_object})
detail_template.html
<p>{{ some_object.chromosome }}</p>
<p>{{ some_object.gene_id }}</p>
<p>View details</p>
Note that the url block above has some_object.id added to it as an argument AFTER the view it goes to in quotations. This is the easiest way.
You can also acheive the same thing with a model method by using the reverse('app_name:view_name', args=[arg_1, arg_2, etc) function, and then call the method with {{ some_object.your_method }}. But those arguments would all depend on what your url patterns and functions took.

Drop down lists not populating in DB in Django Forms on click of submit button

I have a table inside form. It looks like below:
{% extends "base.html" %}
{% block title %}Title{% endblock title %}
{% block content %}
<form actions="" method="post">
{% csrf_token %}
<table>
<table border = "1" cellpadding = "10" cellspacing = "10" bordercolor = "green">
<tr>
<th>numbers</th>
<th>Extension</th>
<th>Vendor</th>
</tr>
{% for number in numbers %}
<tr>
<td>{{ number }}</td>
<td class = "select">Select Extension
<select name="extensions">
{% for obj in sipextensionsets %}
<option value={{obj.sip_extension}}>{{ obj.sip_extension }}</option>
{% endfor %}
</select>
</td>
<td>vendor</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="save"/>
</form>
{% endblock content %}
My forms.py is below:
from django import forms
from .models import column
class didsForm(forms.ModelForm):
class Meta:
model = column
fields = ('extension')
My views.py is below
def saveintodb(request):
try:
instance = coloumn.objects.get(pk=1)
except:
instance = coloumn(pk=1)
instance.save()
if request.method == 'POST':
dids_form = didsForm(data=request.POST['extensions'], instance=instance)
if dids_form.is_valid():
dids_form.save()
messages.success(request, "Settings updated. Please apply settings.")
else:
messages.error(request, "Error: Invalid settings.")
else:
dids_form = didsForm(instance=instance)
return render(request, 'dids/index.html', {'dids_form': dids_form})
In the table, there is a drop down (select tag). I want to save the data into database when user selects something from dropdown and clicks on save button. I know I have mistaken somewhere in views.
You're doing a few things wrong here, unfortunately.
The main problem is that you're passing request.POST['extensions'] as the data argument to your form on POST; but that argument is expecting the whole POST, not a single field.
Linked to that is that you have not used the same name for the field in the model and the field in the form. Although you say in your comment that this is intentional, there doesn't seem to be a reason for it, and it's breaking things. Give them the same name.
Thirdly, you aren't letting Django populate the form, or show any errors when it's not valid. You shouldn't be explicitly passing sipextenionset (although you actually don't seem to be passing that at all, so I'm not sure where it's coming from), and you certainly shouldn't be explicitly iterating. You should let Django display the field:
<td>{{ number }}</td>
<td class="select"><label for="id_extension">Select Extension</label>
{{ form.extension }}
</td>
Finally, I can't at all understand what you are doing with that outer for loop through numbers; you will end up with several values for extension, which is not expected by your form, your model, or your view.

Queryset contenttype show fields name and values in template table

I got a view that receives a model and field the user passes via post (existing model and field inside the app of course) and makes a queryset filter on it, then i need to show in my template that result in a table (name fields must be the column headers) and their respective values.
This what i got so far trying to serialize the queryset result in order to make it easier to show in template:
Views.py:
from django.contrib.contenttypes.models import ContentType
class CommitteeReport(BaseView):
template_name = 'committee/committee_report.html'
def post(self, request, **kwargs):
myfield = request.POST['field'].lower()
my_model = request.POST['model'].lower()
queryset_obj = ContentType.objects.get(model = my_model).model_class().objects.filter(**{myfield:True})
return render(request, self.template_name,{
'requirements': queryset_obj,
})
And my template:
<div class="tab-pane active" id="tab_1">
<table class="datatable table table-striped table-hover" cellspacing="0">
<thead>
<tr>
{% for key in requirements %}
<th>{{ key.fields.name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for item in requirements %}
<tr>{{ item.fields.value }}</tr>
{% endfor %}
</tbody>
</table>
</div>
Thing is, i don't get result or if i change the tag inside the template, i get the objects dictionary for every row.
Any idea of how to achieve what i need ?, thanks in advance.
You can get the list of fields of model using my_instance._meta.local_fields. So you could try this in your view before return:
if queryset_obj.exists():
fields = queryset_obj.first()._meta.local_fields
else:
fields = []
return render(request, self.template_name,{
'requirements': queryset_obj,
'fields': fields,
})
And then using the context variable fields in your template:
<table class="datatable table table-striped table-hover" cellspacing="0">
<thead>
<tr>
{% for field in fields %}
<th>{{ field.name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for req in requirements %}
<tr>
{% for field in fields %}
<td>{{ req|lookup:field.name }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
where you define a custom lookup template filter to fetch the field value:
def lookup(model, attr):
if hasattr(model, attr):
return getattr(model, attr)
else:
return None
Note: I'm assuming that all requirements are the same class and have the same structure, so that requirements.0, the first object in the QuerySet, is used for making the headers of the table.
Note 2: I haven't tried this, you might move some of the work to the view and pass easier variables as context variables (e.g. the list of field names).
Note 3: I haven't added error handling, you should check if model is actually a Model instance for example.

How to switch off Description column in Django Rest Framework Documentation?

Is it possible to switch off Description column in Django Rest Framework Documentation? As you can see on the screenshot below there is a column Description. It is obvious what username and password mean, so I don't need to add more information, however empty cells don't look well. I would like to switch off it only for this method, because for instance in others I would like to have descriptions. Any ideas how can I do this?
There is no easy way to do this at the moment, since that 'Description' column is set at template level. See this template.
To be able to do this in a nicer way you need to customize rest_framework/docs/link.html, maybe have a template tag to check for a custom flag in here:
{% elif link.fields|with_location:'form' %}
<h4>Request Body</h4>
<p>The request body should be a <code>"{{ link.encoding }}"</code> encoded object, containing the following items.</p>
<table class="parameters table table-bordered table-striped">
<thead>
<tr><th>Parameter</th><th>Description</th></tr>
</thead>
<tbody>
{% for field in link.fields|with_location:'form' %}
<tr><td class="parameter-name"><code>{{ field.name }}</code>{% if field.required %} <span class="label label-warning">required</span>{% endif %}</td><td>{% if field.schema.description %}{{ field.schema.description }}{% endif %}</td></tr>
{% endfor %}
</tbody>
</table>
{% endif %}
But I think this is too much to consider just to have that column removed.

grouping fields in django form

I want to render form grouping fields. Form actually is created dynamically according to incoming dictionary
for f in settings.FORM_BIG_FIELDS:
self.fields[f['id']] = eval(f['type'])(label=f['label'], required=f.get('required', True))
self.fields[f['id']].groupp = f.get('group', 1)
groupp attribute means appropriate group, then I try to render it like
{% regroup form.fields.values by groupp as field_group %}
{% for group in field_group %}
<div class="group_{{ group.grouper }}">
{% for field in group.list %}
<p>
{{ field.all }}
{{ field }}
</p>
{% endfor %}
</div>
{% endfor %}
But as output I get the following
<django.forms.fields.CharField object at 0xb527388c>
<django.forms.fields.IntegerField object at 0xb52738ec>
<django.forms.fields.ChoiceField object at 0xb527394c>
I have read that these are not the same as BoundField object. How to render fields or is there any other better approaches to group fields?
If you do not want use any additional libraries, then the most easy solution is to render them manually, i would say. Otherwise you will just spend alot of time repeating the functionality of the library i copied as comment to your post.
There is always the case that things should be DRY. But we build websites for the users and user cares little about how the form rendering in template is done. For this reason we have often created form templates manually like this:
<div class="something">
{{ form.fieldname.label_tag }}{{ form.fieldname }}
</div>
Easyest way to organise it saving you some time. And in my opinion it is not that bad either, since this is not very common when you need fields organised by fieldsets.
I know this question is rather old, but I am sure there are still people who can benefit from a simple solution:
Say you have a group name and list of members. You can define a self.fieldset in your form's init to be a dictionary of {'group_1': ['member_1', 'member_2', ... ], ... }. Once you attach this to the form, you can pass it to views and from there to the template:
In forms.py:
class MyForm:
def __init__(self, current_user, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
self.field['group'].queryset = Group.objects.filter(user = current_user)
...
In views.py:
form = self.Form(current_user)
the_fieldsets = form.fieldset
c = {'form': search_form,
'fieldsets': the_fieldsets }
In your template:
{% for field in form %}
<tr>
<td>{{field.label_tag}}</td>
{% if field.name == 'group' %}
<td>
<select id='{{field.id}}' name='{{field.name}}'>
{% for k,v in fieldsets.items %}
<optgroup label = {{k.name}}>
{% for val in v %}
<option name='{{val}} value = {{val.id}}> {{val.name}} </option> # Note that the select needs to return 'id', so value has to be {{val.id}}
{% endfor %}
</optgroup>
{% endfor %}
</select>
</td>
{% else %}
<td>{{field}}</td>
{% endif %}
<td>{{field.help_text}}</td>
<td>{{field.errors}}</td>
</tr>
{% endfor %}

Categories