Django post duplicates on page refresh - django

I have this view which adds replies to a topic:
#login_required
def post_reply(request, topic_id):
tform = PostForm()
topic = Topic.objects.get(pk=topic_id)
args = {}
if request.method == 'POST':
post = PostForm(request.POST)
if post.is_valid():
p = post.save(commit = False)
p.topic = topic
p.title = post.cleaned_data['title']
p.body = post.cleaned_data['body']
p.creator = request.user
p.user_ip = request.META['REMOTE_ADDR']
p.save()
tid = int(topic_id)
args['topic_id'] = tid
args['topic'] = topic
args['posts'] = Post.objects.filter(topic_id= topic_id).order_by('created')
return render_to_response("myforum/topic.html",args)
else:
args.update(csrf(request))
args['form'] = tform
args['topic'] = topic
return render_to_response('myforum/reply.html', args,
context_instance=RequestContext(request))
The problem is that when user refershes the page after posting a reply her reply is being duplicated. How to avoid this?
UPDATE:Here is the related template:
{% extends "base.html"%}
{% load static %}
{% block content%}
<div class="panel">
<div class="container">
<!-- Posts -->
<div class="col-md-12">
<h3>{{ topic.title }}</h3>
<table class="table table-striped">
<tr class="col-md-9"><td>{{ topic.description }}</td></tr>
<tr class="col-md-3"><div class="userpanel"><td>{{ topic.created }}</td></div></tr>
{% for post in posts %}
<tr class="col-md-12 userpanel"><td>{{ post.title }}</td></tr>
<tr class="">
<td>
<div class="col-md-9 userpanel">{{ post.body }} <br> {{ post.created }} </div>
</td>
<td>
<div class="col-md-3 userpanel">{{ post.creator }} <br> {{ post.created }} </div>
</td>
</tr>
{% endfor %}
</table>
<hr />
<!-- Next/Prev page links -->
{% if posts.object_list and posts.paginator.num_pages > 1 %}
<div class="pagination">
<span class="step-links">
{% if posts.has_previous %}
previous <<
{% endif %}
<span class="current">
Page {{ posts.number }} of {{ topics.paginator.num_pages }}
</span>
{% if posts.has_next %}
>> next
{% endif %}
</span>
</div>
{% endif %}
<a class="button" href="/forum/reply/{{topic.id}}"> Reply </a>
</div> <!-- End col-md-12 -->
</div> <!-- End container -->
</div>
{% endblock %}

Always - I repeat, always, redirect after a POST.
So instead of doing a return render_to_response("myforum/topic.html",args) when your form is valid, do a return HttpResponseRedirect(url_of_your_view) (https://docs.djangoproject.com/en/1.7/ref/request-response/#django.http.HttpResponseRedirect)
Update after OP's comments:
You should use reverse (https://docs.djangoproject.com/en/1.7/ref/urlresolvers/#reverse) to create the url to redirect to:
return HttpResponseRedirect(reverse('myforum.views.topic', args=[topic_id]))
Also, I recommend to name your views (and drop strings for defining them since they are deprecated), so change your urls.py line like this:
from myforum.views import topic
...
url(r'^topic/(\d+)/$', topic, name='view_topic'),
...
And then just do a reverse('view_topic', args=[topic_id]) to get the url of your view.

Related

HTMX form submission produces a duplicate form

{% extends "IntakeApp/base3.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block heading %}
<h2>Allergies for {{request.session.report_claimant}}</h2>
{% endblock %}
{% block content %}
<form hx-post="{% url 'allergy' %}" hx-target="#allergy_target" hx-swap="outerHTML">{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-2 mb-0">
{{ form.allergen|as_crispy_field }}
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
<div class="container-fluid">
<table class="table table-striped table-sm" id="med-table">
<thead>
<tr>
<th>Allergen</th>
</tr>
</thead>
<tbody id="allergy_target">
{% for allergy in allergy_list %}
<tr>
<td>{{allergy.allergen}}</td>
<td>
<form method="POST" action="{% url 'allergy-delete' allergy.id %}">{% csrf_token %}
<input class="btn btn-danger btn-sm" type="submit" value="Delete">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
class AllergyCreateView(generic.CreateView):
model = Allergy
template_name = 'IntakeApp/allergy_form.html'
form_class = AllergyForm
def form_valid(self, form):
form.instance.assessment = Assessment.objects.get(id=self.request.session['assessment_id'])
return super(AllergyCreateView, self).form_valid(form)
def get_success_url(self):
return reverse_lazy("allergy")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
assessment_id = self.request.session['assessment_id']
allergy_list = Allergy.objects.filter(assessment=assessment_id)
context["allergy_list"] = allergy_list
return context
I tried to all the different hx-swap options, but none fix it...It does post correctly and the swap does work, just not sure why I am getting another form in there. Please help
I added the View above. I think thats were my issue is...not sure if I am supposed to be doing this way or not?
Seems like you're trying to render the same html content in your view, which instead should only take a specific element.
You can try to use hx-select="#allergy_target" so htmx will only fetch the table content.

Django: Certain users get random 404 error

I'm facing a strange issue that I can't handle on my own.
In normal cases when users click on a link, then they are directed to a page where they can edit their hook baits (objects). However, certain users get 404 errors, but I don't know why because the page is rendered for most users.
html where the link is
<div class="row justify-content-center mx-2" >
<div class="col-12 p-0">
<ul class="list-group text-center custom-borders m-2 p-0">
{% if own_hookbaits.count == 0 %}
<a href="{% url 'user_profile:hookbaits' request.user.fisherman.fisherman_id %}" class="list-group-item" >No hook baits yet</a>
{% else %}
{% for hookbait in own_hookbaits %}
{{ hookbait.name }}
{% endfor %}
{% endif %}
</ul>
</div>
views.py
class HookBaitUpdateView(UpdateView):
model = HookBait
template_name = "user_profile/hookbaits.html"
form_class = HookBaitForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['formset'] = HookBaitFormset(queryset=HookBait.objects.filter(fisherman=self.request.user.fisherman))
return context
def post(self, request, *args, **kwargs):
formset = HookBaitFormset(request.POST)
if formset.is_valid():
return self.form_valid(formset)
else:
return self.form_invalid(formset)
def form_valid(self, formset):
instances = formset.save(commit=False)
for instance in instances:
instance.fisherman = self.request.user.fisherman
instance.save()
return super().form_valid(formset)
def form_invalid(self, formset):
return HttpResponse("Invalid")
def get_success_url(self):
return reverse('user_profile:profile', args=(self.kwargs['pk'],))
urls.py
app_name = "user_profile"
urlpatterns = [
path("profile/<int:pk>/", views.ProfileView.as_view(), name="profile"),
path("profile/<int:pk>/hookbaits/", views.HookBaitUpdateView.as_view(), name="hookbaits"),
]
rendered html
<div class="row justify-content-center m-0">
<div class="col-12 col-md-6 col-lg-4 p-0">
<div class="row mx-3 my-3 justify-content-center text-center">
<div class="card p-2 custom-borders">
<div class="card-body p-2">
<form method="POST">
{% csrf_token %}
<table class="d-flex justify-content-center">
{{ formset.management_form }}
{% for form in formset %}
<tr class="formset_row">
{% for field in form.visible_fields %}
<td class="pb-2">
{% if form.instance.pk %}{{ form.DELETE }}{% endif %}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" class="btn btn-primary w-50 mt-1" style="background-color: #00754B;" value="Mentés">
</form>
</div>
</div>
</div>
</div>
</div>
Any suggestions what the solution would be? Thanks!
there are some possibilities
the wrong link clicked like if your trying access this URL profile/25/hookbaits/
and in the data index there is no HookBait with the id of 25
in HookBaitUpdateView you are trying to get queryset=HookBait.objects.filter(fisherman=self.request.user.fisherman)
maybe there is no hookbait associate with user.fisherman
404 page mostly served when you call get_object_or_404(HookBait, pk=25)
and update view may call this method

Using a different counter from different object_list than the one used in for_loop

I want to count the objects in my template, but since I am using pagination to divide them into pages, I want to use counter from different object_list than the one used in the for loop.
This is my template:
{% extends 'base.html' %}
{% block content %}
{% if user.is_superuser %}
<div class="row">
<div class="col-sm-4 offset-sm-4">
<form class="form-inline md-form form-sm" method="GET" action="">
<i class="fa fa-search" aria-hidden="true"></i>
<input class="form-control form-control-sm ml-3 w-75" type="text" placeholder="搜索货品 Znajdź część" name="q" value="{{ request.GET.q }}"/>
</form>
</div>
</div>
<br/>
<hr/>
<div class='row'>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Image</th>
<th scope="col">Product name</th>
<th scope="col">Price (cost)</th>
<th scope="col">Price (PL)</th>
</tr>
</thead>
<tbody>
{% for product in objects %}
<tr>
<th scope="row">{{forloop.counter}}</th>
<td><img class="img img-fluid rounded" width="200" height="136" src="{{ product.photo.url }}"></td>
<td>{{product.product_name}}</td>
<td>{{product.price}}</td>
<td>{{product.price_pol}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="col-sm-4 offset-sm-4" align="center">
<div class="pagination">
<span class="step-links">
{% if objects.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ objects.number }} of {{ objects.paginator.num_pages }}.
</span>
{% if objects.has_next %}
next
last »
{% endif %}
</span>
</div>
</div>
{% endif %}
{% endblock content %}
My views.py:
Here I would like to use counter from object_list and not object
(Note the context in productview)
#login_required
def productview(request):
object_list = Product.objects.all().order_by('-date')
query = request.GET.get('q')
if query:
object_list = object_list.filter(
Q(product_name__icontains=query)|
Q(date__icontains=query)
).distinct()
paginator = Paginator(object_list, 5)
page = request.GET.get('page')
objects = paginator.get_page(page)
return render(request, 'products_list.html', {'objects': objects, 'object_list':object_list})
I wanted to use the nested loop, but it didn't work out. Is there another way that I don't know of? If I leave it like this every time I click nextpage the counter will start from "1". I would like the counter to continue when I click the next page...
You can enumerate you obect_list:
def productview(request):
object_list = Product.objects.all().order_by('-date')
query = request.GET.get('q')
if query:
object_list = object_list.filter(
Q(product_name__icontains=query)|
Q(date__icontains=query)
).distinct()
object_list =list(enumerate(object_list))
paginator = Paginator(object_list, 5)
page = request.GET.get('page')
objects = paginator.get_page(page)
return render(request, 'products_list.html', {'objects': objects, 'object_list':object_list})
Then the structure of the objects has changed: product.0 will contain the counter and product.1 will contain the model instance:
{% for product in objects %}
<tr>
<th scope="row">{{product.0}}</th>
<td><img class="img img-fluid rounded" width="200" height="136" src="{{ product.1.photo.url }}"></td>
<td>{{product.1.product_name}}</td>
<td>{{product.1.price}}</td>
<td>{{product.1.price_pol}}</td>
</tr>
{% endfor %}

Django and HTML template : Group by panels with common object attributes

One more time, I come back to you in order to get advices or your help.
I'm displaying in my django template a list of objects and I would like to sort them through a common attributes : category.
Each object displayed (a publication) gets some attributes : category, format, language ...
For example :
The white text with blue background indicates the category. I have 2 publications with category = BIOLOGICAL STANDARDISATION PROGRAMME and 1 publication with category = TEST
I would like to group both BIOLOGICAL STANDARDISATION PROGRAMME in one panel but I don't find a way to do that.
This is my HTML template file :
{% for element in test_research|dictsort:"publication.category.name" %}
<div class="col-sm-12">
<div class="panel panel-default request-panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title">
{{ element.publication.category }}
</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-9">
<p class="request-publication">{{ element.publication }} </p>
</div>
<div class="col-sm-3 request-cover">
{% if element.publication.cover %}
<a href="{{ element.publication.cover.url }}" target="_blank">
{% thumbnail element.publication.cover "40x40" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}</a>
{% endif %}
</div>
</div>
</div>
<div class="panel-footer">
<div class="row">
<table>
<tbody>
<tr>
<td class="col-md-1">
<div class="material-switch pull-right">
<input id="someSwitchOptionSuccess_{{ element.id }}" name="DocumentChoice" type="checkbox"
value="{{ element.id }}"/>
<label for="someSwitchOptionSuccess_{{ element.id }}" class="label-success"></label>
</div>
</td>
<td class="col-md-1 request-language"> {{ element.language }}</td>
<td class="col-md-1 request-format">
{% if element.format == 'pdf' %}
<span class="badge alert-danger">{{ element.format }}</span>
{% endif %}
{% if element.format == 'epub' %}
<span class="badge alert-info">{{ element.format }}</span>
{% endif %}
</td>
<td class="col-md-1 request-flag">
{% if element.publication.new_publication == True %}
<span class="glyphicon glyphicon-flag"></span>
{% else %}
<span></span>
{% endif %}
</td>
<td class="col-md-offset-5 col-md-3 text-right">{{ element.title }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endfor %}
And in my views.py file :
def get_context_data(self, **kwargs):
search_category = Document.objects.values_list('publication__category__name', flat=True).distinct()
kwargs['search_category'] = search_category
search_format = Document.objects.values_list('format', flat=True).distinct()
kwargs['search_format'] = search_format
search_language = Document.objects.values_list('language', flat=True).distinct()
kwargs['search_language'] = search_language
checkbox_category = self.request.GET.getlist('CategoryChoice')
checkbox_format = self.request.GET.getlist('FormatChoice')
checkbox_language = self.request.GET.getlist('LanguageChoice')
choice_title = self.request.GET.get('TitleChoice')
kwargs['checkbox_category'] = checkbox_category
kwargs['checkbox_format'] = checkbox_format
kwargs['checkbox_language'] = checkbox_language
kwargs['choice_title'] = choice_title
# default to all documents
test_research = Document.objects.all().order_by('publication__category__name')
kwargs['test_research'] = test_research
if "SubmitChoice" in self.request.GET:
test_research = Document.objects.all()
# if user entered any search criteria, add those filters
if checkbox_category:
test_research = test_research.filter(publication__category__name__in=checkbox_category)
if checkbox_format:
test_research = test_research.filter(format__in=checkbox_format)
if checkbox_language:
test_research = test_research.filter(language__in=checkbox_language)
if choice_title:
test_research = test_research.filter(
Q(title__icontains=choice_title) | Q(publication__title__icontains=choice_title))
kwargs['test_research'] = test_research
return super(HomeView, self).get_context_data(**kwargs)
I can add models.py file if necessary. How it's possible to group them under the same category panel ?
EDIT :
I maybe found something with that :
{% for category in checkbox_category %}
<div class="col-sm-12">
<div class="panel panel-default request-panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title">
{{ category }}
</h4>
</div>
{% for element in test_research %}
{{ element.publication.category }} - {{ category }}
{% if element.publication.category == category %}
But the if condition doesn't seems to work even if {{element.publication.category}} == {{category}}
You should restructure your data in the view so that it's already prepared for the template. Django's template system is built in a way to avoid this type of logic.
You might be able to do it simply like this:
from collections import defaultdict
research_categories = defaultdict(list)
for element in test_research:
research_categories[element.publication.category].append(element)
Then use research_categories in your template.

Django paginator not providing content on next page

I use Django paginator to break my search results to few pages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
In my view I perform the search based on the output from the form
#login_required
def payment_range_list(request):
#import pdb; pdb.set_trace()
title = 'payment list'
#import pdb; pdb.set_trace()
if request.method == "POST":
form = PaymentRangeForm(request.POST)
#import pdb; pdb.set_trace()
if form.is_valid():
start_date = form.cleaned_data['start_date']
end_date = form.cleaned_data['end_date']
building = form.cleaned_data['building']
payment_list = LeasePaymentFilter(request.GET, queryset=LeasePayment.objects.filter(payment_date__range=[start_date, end_date],lease__unit__building = building ))
paginator = Paginator(payment_list, 25) # Show 25 contacts per page
page = request.GET.get('page')
try:
payment_page = paginator.page(page)
except PageNotAnInteger:
payment_page = paginator.page(1)
except EmptyPage:
payment_page = paginator.page(paginator.num_pages)
else:
payment_list = None
payment_page = None
start_date = None
end_date = None
building = None
else:
payment_list = None
payment_page = None
start_date = None
end_date = None
building = None
#form = PaymentRangeForm()
form = PaymentRangeForm(initial = {'building': 0 })
return render(request,'payment/payment_range.html', {'form':form, 'payment': payment_list,'page_payment':payment_page, 'start_date':start_date, 'end_date':end_date, 'building':building })
On the top of the page I have a form that I use for search and 2nd part is used to display the results.
{% extends "rent_base.html" %}
{% load crispy_forms_tags %}
{% block title %}
Lease List
{% endblock title %}
{% block content %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-10 col-sm-offset-1 hid">
<h4 class="display-4">List > Payments> by Date Range </h4><div style="float:right;"> CSV for current range payments</div>
</div>
</div>
<div class="row">
<div class="col-sm-10 col-sm-offset-1 hid">
<form method="POST" class="form" action="" method="get">
{% csrf_token %}
{{ form|crispy}}
<br>
<button type="submit" class="btn btn-primary btn-primary">Search</button>
</form>
<br>
<b>Start date:</b> {{ start_date }} <b>End date:</b>{{ end_date }}
<br>
<table class="table table-striped">
<tr>
<th># Payment</th>
<th>Payment type</th>
<th>Amount </th>
<th>Deposit</th>
<th>Lease</th>
<th>Unit</th>
<th>Building</th>
<th>Term</th>
<th>Payment date</th>
<th>Payment method</th>
</tr>
{% for payment in page_payment %}
<tr>
<td> {{ payment.id }}</td>
<td> {{ payment.get_payment_type_display }}</td>
<td> {{ payment.amount }}</td>
<td>
{% if payment.is_deposit %}
<span class="glyphicon glyphicon-ok text-success" aria-hidden="true" ></span>
{% else %}
<span class="glyphicon glyphicon-remove text-warning" aria-hidden="true" ></span>
{% endif %}
</td>
<td> {{ payment.lease }}</td>
<td> {{ payment.lease.unit.number }}</td>
<td> {{ payment.lease.unit.building.name }}</td>
<td> {{ payment.leaseterm }}</td>
<td> {{ payment.payment_date }}</td>
<td> {{ payment.get_method_display }}</td>
</tr>
{% endfor %}
</table>
<div class="pagination">
<span class="step-links">
{% if page_payment.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ page_payment.number }} of {{ page_payment.paginator.num_pages }}.
</span>
{% if page_payment.has_next %}
next
{% endif %}
</span>
</div>
</div>
</div></div>
{% endblock content %}
My problem is that when my search returns amount of results more than 1 page and I click on next pages. The next pages are always empty.
Any feedback where my problem is?
The problem is that your serch form is POST. So when you click next button you send GET request and if request.method == "POST": part of you view is not triggered.
I suggest you to change serch form to GET:
<form method="GET" action="">
<input type="text" name="q" placeholder="Search" value="{{ request.GET.q }}">
<input type="submit" value="Search">
</form>
And change next button to this:
{% if payment.has_next %}
next
{% endif %}
This will save search value in address bar even after you move to the next page.