I have this view to show the previously populated data, which was only populated in the admin panel:
from .models import (Token,
Sell,
LogisticCost,
IncomeCost,
FinalPayment,
CustomerServiceCost,
Fatura)
def product_list(request):
context = {'product_list': ProductList.objects.filter(client=request.user.id).all(),
'data_payment': Fatura.objects.all()}
return render(request, 'Clientes/list_products.html', context)
This is the view to update these values:
class UpdateProduct(UpdateView):
model = ProductList
context_object_name = 'product_list'
template_name = 'Clientes/update_product.html'
fields = ['name', 'description', 'cost_price', 'sell_price', 'ncm']
My form in the update page is:
<form method="post" action="{% url 'client:product_list' %}">
{% csrf_token %}
{{ form }}
<button class="btn btn-inverse" type="submit">Atualizar</button>
</form>
My update page is working as expected, showing every value related to the selected object, but when I submit the form the model has not changed. What is happening to not update the values?
You are submitting the form to the product_list view - you should submit it to the update view instead.
Depending on your URLs, the form should look something like:
<form method="post" action="{% url 'client:update_product' product_list.pk %}">
After all, I removed the action from the form and I added the variable success_url = reverse_lazy('client:product_list') to my UpdateView. That solved the problem.
Related
I'm making a simple blog app. I have added the ability to "like" a post on your feed. However, the only way I can figure out closing a view is by returning some form of redirect. The problem is, if the post you're "liking" is halfway down the page, I don't want it to reset the zoom to the top of the page again. Is there a way simply to redirect to the same page without affecting zoom?
Here's my post model:
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="author")
likes = models.ManyToManyField(User, related_name="likes", blank=True)
def like(self, post):
self.likes.add(post)
def unlike(self, post):
self.likes.remove(post)
I have the following setup in Views:
#login_required
def like(request, pk):
post = Post.objects.get(id=pk)
Post.like(post, request.user)
return HttpResponseRedirect(reverse('Home'))
#login_required
def unlike(request, pk):
post = Post.objects.get(id=pk)
Post.unlike(post, request.user)
return HttpResponseRedirect(reverse('Home'))
Here's how I'm calling the Views from my URLs:
path('like/<int:pk>', views.like, name='Like'),
path('unlike/<int:pk>', views.unlike, name='Unlike'),
I'm using a form on my template to trigger the URL:
{% if user in post.likes.all %}
<form action="{% url 'Unlike' post.pk %}" method="POST">
{% csrf_token %}
<button type="submit" value="{{ post.id }}" class="unlike">UNLIKE</button>
</form>
{% else %}
<form action="{% url 'Like' post.pk %}" method="POST">
{% csrf_token %}
<button type="submit" value="{{ post.id }}" class="like">LIKE</button>
</form>
{% endif %}
Is there something I can change? I'm assuming there's something I'll need to change or add under "return" in my view functions?
One way to do this would be adding an anchor tag to each like button that uses the post ID, then when redirecting to the post from your view, include #pk at the end of the post URL to direct to that ID anchor down the page.
I'm trying to get validation running on a django form used to retrieve a list of objects in a ListView View. Despite having read django docs and many other questions here, I can't find out what's wrong in this simple test code:
form.html
<form action="list.html" method="get">
{{ form }}
<input type="submit" value="Submit">
</form>
list.html
<ul>
{% for area in object_list %}
<li>{{ area.name }}</li>
{% endfor %}
</ul>
forms.py
from django import forms
class SearchArea(forms.Form):
area = forms.CharField(label='Area code', max_length=6)
def clean_area(self):
area = self.cleaned_data['area'].upper()
if '2' in area:
raise forms.ValidationError("Error!")
return area
views.py
class HomePageView(FormView):
template_name = 'form.html'
form_class = SearchArea
class AreaListView(ListView):
template_name = 'list.html'
model = AreaCentral
def get_queryset(self):
q = self.request.GET.get('area')
return AreaCentral.objects.filter(area__istartswith=q)
When I try to submit something like "2e" I would expect a validation error, instead the form is submitted. Moreover I can see in the GET parameters that 'area' is not even converted to uppercase ('2E' instead of '2e').
The default a FormView will only process the form on POST; the GET is for initially displaying the empty form. So you need to use method="post" in your template form element.
Your action attribute is also suspect; it needs to point to the URL of the form view. If that actually is the URL, note it's not usual to use extensions like ".html" in Django URLs, and I would recommend not doing so.
Im new to django and was struck by using slug, now Im confused how to use the ID parameter and convert to slug
URL.py
url(r'^deletePost/(?P<slug>[\w-]+)/$', views.delete_post, name='delete_post')
Template
<form method="POST" action="{% url 'delete_post' id=post.id %}">{% csrf_token %}
<button type="submit" class="btn btn-danger">   Delete</button>
</form>
Views.py
def delete_post(request,slug):
posts=Post.objects.get(slug=slug)
if request.method == 'POST':
posts.delete()
return redirect("home")
How can i use slug & id to delete the post which is created
Any help is appreciated. Thanks in advance
Error for reference
In my opionion, you dont want to convert the id to slug. You can just make your application flexible enough so that you could delete by either slug or id. You just need to handle the parameters accordingly.
So, you can do something like this:
urls.py
url(r'^deletePost/(?P<slug>[\w-]+)/$', views.delete_post, name='delete_post_by_slug'),
url(r'^deletePost/(?P<id>[0-9]+)/$', views.delete_post, name='delete_post_by_id')
And in the views:
def delete_post(request, slug=None, id=None):
if slug:
posts=Post.objects.get(slug=slug)
if id:
posts=Post.objects.get(id=id)
#Now, your urls.py would ensure that this view code is executed only when slug or id is specified
#You might also want to check for permissions, etc.. before deleting it - example who created the Post, and who can delete it.
if request.method == 'POST':
posts.delete()
return redirect("home")
Note that you can compress the 2 URL patterns into a single one - but this approach keeps it readable, and understandable. I shall let you figure out the URL consolidation once you are comfortable with the django framework, etc..
If you want to use both slug and id, your URL pattern should look like this:
url(r'^deletePost/(?P<slug>[\w-]+)-(?P<id>[0-9]+)/$',
views.delete_post, name='delete_post')
And your view should look like this:
def delete_post(request, **kwargs):
# Here kwargs value is {'slug': 'qw', 'id': '1'}
posts = Post.objects.get(**kwargs)
if request.method == 'POST':
posts.delete()
return redirect('home')
# ... (I guess this view does not end here)
And your template also have to set both:
<form method="POST" action="{% url 'delete_post' slug=post.id id=post.id %}">{% csrf_token %}
<button type="submit" class="btn btn-danger">   Delete</button>
</form>
I am totally stuck to understand this behaviour and found a workaround I don't really like. Can anyone help enlighten me please? The context is I have a bootstrap styled form to create new records (inheriting from the generic.CreateView)
url.py:
url(r'^$', home, name='home'),
url(r'^main/$', views.MainView.as_view(), name='MainView'),
url(r'^topic/(?P<pk>[0-9]+)/$', catalogue_views.TopicView.as_view(), name='TopicView'),
url(r'^resource/(?P<pk>[0-9]+)/$', catalogue_views.DetailView.as_view(), name='ResourceDetail'),
url(r'^contribute/$', catalogue_views.ContributeView.as_view(success_url="/main/"), name='Contribute'),
views.py:
class ContributeView(generic.CreateView):
template_name = "openeye/contribute.html"
form_class = ContributeForm
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ContributeView, self).dispatch(*args, **kwargs)
class MainView(generic.ListView):
template_name = "openeye/main.html"
context_object_name = 'topic_list'
# TODO Make this only active topic areas?
def get_queryset(self):
return TopicArea.objects.all().order_by('name')
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(MainView, self).dispatch(*args, **kwargs)
forms.py:
class ContributeForm(forms.ModelForm):
class Meta:
model = CatalogueItem
fields = ['title', 'topic_area', 'description', 'link', 'what_learn', 'how_apply', 'level', 'relevant_to', 'discovered_by']
ROLE_CHOICES = [[x.id, x.job] for x in JobType.objects.all()]
title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'To sell this resource to others'}), max_length=80, required=True)
description = forms.CharField(widget=forms.Textarea(attrs={'rows': 2, 'placeholder': 'Clear, e.g. format, duration, activities...'}))
link = forms.CharField(widget=forms.URLInput(attrs={'placeholder': 'If required, link to resource http://...'}), required=False)
what_learn = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, 'placeholder':"This is important,."}), label='What will you learn?')
how_apply = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, 'placeholder':"How could this be put into action afterwards?"}), label='How could you apply this?')
relevant_to = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=ROLE_CHOICES)
and a template with a form:
<div class="container">
<div class="entry-form row">
<div class="col-md-10 col-md-offset-1 col-sm-10 col-sm-offset-1 col-xs-10 col-xs-offset-1">
<form action="{% url 'MainView' %}" method="post" class="form">
<input type="hidden" name="next" value="{{ next }}">
{% bootstrap_form form %}
<button class="btn btn-primary btn-lg" type="submit">Submit Suggestion</button>
{% csrf_token %}
</form>
</div>
</div>
The form works perfectly and the data is saved nicely into the database. The problem is afterwards, the browser goes to the correct URL /main/ but the SCREEN IS BLANK. The server shows HTTP 405 0, and if I refresh the page it works.
If I alter the template so the action="{% url 'Contribute' %}" to return to the same form, I get HTTP 500 and a Django message about 'No URL to redirect to'. So two different errors determined by the re-direct location. In both case if I just click in browser url field and hit return it works.
I am sure this worked initially and then broke but I solved it as follows. Hard code in the success_url using it's path
url(r'^contribute/$', catalogue_views.ContributeView.as_view(success_url="/main/"), name='Contribute'),
Removing any action link in the template:
<form action="" method="post" class="form">
Is this the correct approach? Why, despite going to the correct URLs, do the pages not load or give errors with my original approach? I'd love to understand this.
Are you sure your data is actually saved on the server? From what you posted, it seems very unlikely. Here is the normal process followed by Django:
GET on form view (ContributeView)
→ returns an empty form
POST on form view (ContributeView)
→ if invalid, go back to step 1. If valid return a 302 Redirect to success_url.
GET on success_url
So normally, in your template, the form action should be empty, so the form gets posted back to the view that generated it. And the ContributeView should have a success url that redirects to wherever you want to send the user after:
from django.core.urlresolvers import reverse_lazy
class ContributeView(generic.CreateView):
# other stuff
success_url = reverse_lazy('MainView')
The behavior you get, with the 405, is because the browser, attempts to send the form directly to MainView which, not being a form view, tells the browser it does not know how to handle a POST method.
I am working with django forms and am using django bootstrap form(https://django-bootstrap-form.readthedocs.org/en/latest/) for UI. I am able to create forms in html using the django bootstrap form. Now the problem is that i want to edit the forms and update the record in the database.
My question is how can i use the django bootstrap form to provide a form for editing
for eg:
i am using
<form role="form" action="/abc/" method="POST">{% csrf_token %}
<div class="form-group">
{{ form.name.errors }}
{{ form.name|bootstrap }}
</div>
</form>
this is the form when filling for the first time. When i click on the edit option i want the same UI but with value="the_value_saved_in_the_database" something like
{{ form.name|bootstrap value="_data_"}}
How can i achieve it?
Hope you understand the problem.
Thanks in advance
You need to load the form with data (called binding the form) before you render it. If the form represents some data that you have stored in the model, then create a ModelForm and pass in the model instance for which you want to edit the data.
Here is an example:
class AddressBook(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
class AddressForm(forms.ModelForm):
class Meta:
model = AddressBook
def edit_address(request, pk=None):
existing_entry = AddressBook.objects.get(pk=pk)
form = AddressForm(instance=existing_entry)
return render(request, 'edit.html', {'form': form})
In urls.py:
url('^address/edit/(?P<pk>\d+)$', 'edit_address', name="edit"),
url('^address/save/$', 'save_address', name="save"),
Now, when you call your view http://localhost:8000/address/edit/1, the form will be populated by the data for the entry whose primary key is 1 ready for editing. In your template, simply render the form, and point it to the save view:
<form method="post" action="{% url 'save' %}">
{% csrf_token %}
{{ form|bootstrap }}
</form>
If you are going to be doing this often, its easier to use the generic class based views (like CreateView, EditView) to simplify your code.