I applied django-filter library in my project if it's not filtering the items.
if i visit the url http://127.0.0.1:8000/products/?q=&category=electronics it should gives the only electronics products but its giving the all available products. What i am doing wrong? it should filter categories wise or product title wiase and price wise.
class ProductFilter(FilterSet): # by using django-filters
title = CharFilter(field_name='title', lookup_expr='icontains', distinct=True)
category = CharFilter(field_name='categories__title', lookup_expr='icontains', distinct=True)
category_id = CharFilter(field_name='categories__id', lookup_expr='icontains', distinct=True)
min_price = NumberFilter(field_name='price', lookup_expr='gte', distinct=True)
max_price = NumberFilter(field_name='price', lookup_expr='lte', distinct=True)
class Meta:
model = Product
fields = ['category', 'title', 'description', 'min_price', 'max_price']
class FilterMixin(object):
filter_class = ProductFilter
search_ordering_param = 'ordering'
def get_queryset(self, *args, **kwargs):
try:
qs = super(FilterMixin, self).get_queryset(*args, **kwargs)
return qs
except:
raise ImproperlyConfigured("You must have a queryset in order to use the FilterMixin")
def get_context_data(self, *args, **kwargs):
context = super(FilterMixin, self).get_context_data(*args, **kwargs)
qs = self.get_queryset()
ordering = self.request.GET.get(self.search_ordering_param)
if ordering:
qs = qs.order_by(ordering)
filter_class = self.filter_class
print(filter_class)
if filter_class:
f = filter_class(self.request.GET, queryset=qs)
context['object_list'] = f
return context
class ProductListView(FilterMixin, ListView):
queryset = Product.objects.all()
filter_class = ProductFilter
def get_context_data(self, *args, **kwargs):
context = super(ProductListView, self).get_context_data(*args, **kwargs)
context['filter_form'] = ProductFilterForm(data=self.request.GET or None)
return context
Template File-
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load static %}
{% block content %}
<div class='col-sm-2'>
<form method="GET" action="{% url 'products:product-list' %}">
{{ filter_form|crispy }}
<input type='hidden' name='q' value='{{ request.GET.q }}' />
<input type='submit' value='Apply Filter' class='btn btn-default'>
</form>
Clear Filters
</div>
<div class='col-sm-12'>
<h3>All Products <small>*Categories*</small></h3>
{% if product_list %}
<div class="row">
{% for product in product_list %}
<div class="col">
{% include 'products/snippets/card.html' with instance=product %}
</div>
{% endfor %}
{% else %}
<p>No product found!!</p>
{% endif %}
</div>
</div>
{% endblock %}
Your FilterMixin never exports something like a product_list, so that will be the original queryset, not the one that you filter with the FilterSet.
You can easily update this with:
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
qs = context['object_list']
if self.filter_class:
qs = self.filter_class(self.request.GET, queryset=qs).qs
ordering = self.request.GET.get(self.search_ordering_param)
if ordering:
qs = qs.order_by(ordering)
context['object_list'] = qs
context_object_name = self.get_context_object_name(qs)
if context_object_name is not None:
context[context_object_name] = qs
return context
Related
I ran into a problem I have questions that are related to items_buy_id , there are also choices that are related to question_id questions
Questions items_buy_id It turns out to connect
And with the choice you will not contact as it should
My models.py
from django.db import models
from datetime import datetime
from phonenumber_field.modelfields import PhoneNumberField
from django_resized import ResizedImageField
from email.policy import default
from django.utils.translation import gettext_lazy
class Items_buy(models.Model):
class Meta:
db_table = 'items_buy'
verbose_name = 'Телефон который покупаем'
verbose_name_plural = 'Телефоны которые покупаем'
image_phone = ResizedImageField(size=[100,100], upload_to='for_sell/',verbose_name='Фотография модели телефона')
model_produkt = models.TextField(max_length=80, verbose_name='Модель продукта ')
text = models.TextField(max_length=500, verbose_name='Текст')
max_prise_iphone = models.FloatField(verbose_name='Максимальная цена telefoha')
image_phone_for_buy_bord = ResizedImageField(size=[100,100],upload_to='for_sell/',verbose_name='Фотография модели телефона ha prodazy')
def __str__(self):
return self.model_produkt
class Question(models.Model):
class Meta:
db_table = 'question'
verbose_name = 'Вопрос к телефону'
verbose_name_plural = 'Вопросы к телефону'
items_buy_id = models.ForeignKey(Items_buy, on_delete=models.RESTRICT)
title = models.CharField(max_length=150,verbose_name='Заголовок вопросa')
question_text =models.TextField(max_length=100, verbose_name='Заголовок вопросa text')
max_prise_qustion = models.FloatField(verbose_name='Максимальная цена')
def __str__(self):
return self.title
class Choice(models.Model):
class Meta:
db_table = 'choice'
verbose_name = 'Выбор ответа'
verbose_name_plural = 'Выбор ответов'
#items_buy_id = models.ForeignKey(Items_buy, on_delete=models.RESTRICT)
question_id = models.ForeignKey(Question, on_delete=models.RESTRICT)
title = models.CharField(max_length=1000, verbose_name='Заголовок выбора')
points = models.FloatField(verbose_name='Цена ответа')
#lock_other = models.BooleanField(default=False, verbose_name='Смотреть другой вариант ответа')
def __str__(self):
return self.title
My urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('',views.home, name ='home'),
path('sell_iphone/', views.sell_iphone, name = 'sell_iphone'),
path('sell_iphone_page/<int:pk>/', views.sell_iphone_page, name= 'sell_iphone_page'),
path("getqestion/<int:pk>/", views.getqestion, name = 'getqestion'),
]
My html
{% load static %}
{% block content %}
<head>
<link rel="stylesheet" href="{% static 'css/qestion.css' %}" type="text/css">
</head>
<body>
{% include 'navbar.html' %}
<div class="bar">
{% for question in test %}
<div class="bar_infor_bar">
<div class="bar_infor_bar_title">{{question.title}} </div>
<div class="wraper_sell_in_line_img_class2_qestion_text">{{question.question_text}}</div>
{% for choiceses in choice %}
<div class="bar_infor_button_nav">
<button class="bar_infor_button">{{choiceses.title}}</button>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
</body>
{% endblock %}
My views.py
from django.shortcuts import render, redirect
from .models import Items_buy, Question, Choice, Answer, Orders
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
def home(request):
return render(request, 'home.html')
def sell_iphone(request):
limit = request.GET.get('limit')
if limit == None:
limit = 40
limit = int(limit)
iphone = Items_buy.objects.filter()
count = iphone.count()
page = request.GET.get('page')
paginator = Paginator(iphone, 1)
try:
iphone = paginator.page(page)
except PageNotAnInteger:
page = 1
iphone = paginator.page(page)
except EmptyPage:
page = paginator.num_pages
iphone = paginator.page(page)
#pages = list(range(1, (paginator.num_pages + 1)))
iphone = Items_buy.objects.all()
#iphone = iphone[0:limit]
context = {'iphone':iphone, 'count':count, 'paginator':paginator, }
return render(request, 'sell_iphone.html', context)
def sell_iphone_page(request,pk ):
iphones = Items_buy.objects.filter(id=pk)
#question = Question.objects.all()
context = {'iphones':iphones, }
return render(request, 'sell_iphone_page.html', context)
`def getqestion( request, pk):
test = Question.objects.filter(items_buy_id = pk)
choice = Choice.objects.filter(question_id = pk)
context = {'test':test,'choice':choice}
return render(request, 'getqestion.html', context)`
I'm having a problem with the def getqestion function. I linked the question to the product, but the answer to the question didn't work at all
When using select choice = Choice.objects.filter(question_id = pk)
enter image description here
When using select choice = Choice.objects.all()
enter image description here
When using select choice = Choice.objects.filter(id = pk)
enter image description here
And you need 1.test1 to include: da, net, HY TAKOE
And 1.test2 included: 2,1,3
Thanks in advance to anyone who can suggest how to do this!!!
In my case it helped
views.py
class getqestion(generic.DetailView):
model = Items_buy
template_name = 'getqestion.html'
context_object_name = 'testing'
html
<div>
{% for testings in testing.question_set.all %}
<h1>{{testings.titles}}</h1>
<h3>{{testings.question_text}}</h3>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
{% csrf_token %}
<form action="{% url 'vote' testings.id%}" method="post">
{% for choice in testings.choice_set.all %}
<input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{choice.title}}</label>
{% endfor %}
{% endfor %}
</div>
<input type="submit" value="vote">
</form>
I used the modelformset_factory method for multiple photo uploads. But when I want to update the post, I don't know how to do this, I tried something, but this only serves to re-upload photos and upload the uploaded photos again. How can I update or delete a previously uploaded photo? Also, I cannot access the photos in formset as {{formset.image}}. How can I access this?
Views.py
def post_update(request, slug):
post = get_object_or_404(Post, slug=slug)
if not request.user.is_authenticated and not request.user == post.seller or not request.user.is_admin:
raise Http404
ImageFormSet = modelformset_factory(PostImage, form=PostImageForm, extra=5, can_delete = True)
form = PostForm(data=request.POST or None, instance = post, files = request.FILES or None)
formset = ImageFormSet(data=request.POST or None, files= request.FILES or None, queryset=PostImage.objects.filter(post__slug=slug))
if form.is_valid():
if formset.is_valid():
for forms in formset.cleaned_data:
if forms:
image = forms['image']
print(forms['image'])
photo = PostImage(post=post, image=image)
photo.save()
form.save(commit=True)
messages.success(request,"Success")
return HttpResponseRedirect(reverse('gayrimenkul:detail',kwargs={'slug':form.instance.slug}))
return render(request,'post_update.html',{'form':form,'formset':formset,'slug':slug})
post_update.html
{% extends "main_page.html" %}
{% load static %}
{% block icerik %}
{% load crispy_forms_tags %}
<div class="row">
<div class="container">
<h2 class="page_header">İlan Ver</h2>
<hr>
{% if form.errors %}
{{form.errors}}
{% endif %}
<div class="col-md-12">
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{form.media}}
{{form|crispy}}
<div class="row">
<div class="col-md-6">
{{formset}}
</div>
<div class="col-md-6">
<button type="submit" id="inp" class="btn btn-outline-primary" style="float:right;">Kaydet</button>
</div>
</div>
</form>
</div>
</div>
</div>
{% endblock %}
Creating posts works successfully.
def post_create(request):
if not request.user.is_authenticated:
raise Http404
ImageFormSet = modelformset_factory(PostImage, form=PostImageForm, extra=5)
if request.method == 'POST':
post_form = PostForm(request.POST, request.FILES)
formset = ImageFormSet(request.POST, request.FILES, queryset=PostImage.objects.none())
if post_form.is_valid():
created_post = post_form.save(commit=False)
created_post.seller = request.user
created_post.save()
for form in formset.cleaned_data:
if form:
image = form['image']
photo = PostImage(post=created_post, image=image)
photo.save()
messages.success(request,'Success')
return HttpResponseRedirect(reverse('gayrimenkul:detail',kwargs={'slug':created_post.get_slug()}))
else:
post_form = PostForm()
formset = ImageFormSet(queryset = PostImage.objects.none())
return render(request,'post_create.html',{'form':post_form,'formset':formset})
Before checking if the form is valid just filter the model of the formset
data = PostImage.objects.filter(post=post)
Give index of the item for a formset item starting form 0 and (f)the item itself and if the item of the id is not changed than save previous item itself else change the database image with the new image you have updated.
if formset.is_valid():
for index, f in enumerate(formset):
if f.cleaned_data:
if f.cleaned_data['id'] is None:
pic = PostImage(post=post, image=f.cleaned_data.get('image'))
pic.save()
elif f.cleaned.data['image'] is False:
pic = PostImage.objects.get(id = request.POST.get('form-' + str(index) + '-id'))
pic.delete()
else:
pic = PostImage(post=post, image=f.cleaned_data.get('image'))
d = PostImage.objects.get(id=data[index].id) #get slide id which was uploaded
d.image = pic.image # changing the database title with new title
d.save()
This might work for you or get you some idea.
I'm having some trouble getting this validation to work. I want the form to only submit if there's a valid .yaml, .yml, or .json at the end of the URL I'm submitting.
forms.py:
class TemplateForm(FlaskForm):
template_url = URLField( 'Template URL', validators=[InputRequired(), URL(message='error')])
submit = SubmitField('Add Template')
def validate_filetype(self, field):
form = TemplateForm
template_path = form.template_url
ext = os.path.splitext(template_path)[1]
if ext not in ('.json', '.yml', '.yaml'):
raise ValidationError('Invalid File Type')
views.py:
#apps.route('/new-template', methods=['GET', 'POST'])
#login_required
#admin_required
def new_template():
"""Add a new app."""
form = TemplateForm()
if form.validate_on_submit():
template_location = form.template_url.data
flash("added template: " + template_location)
template_path = urlparse(template_location).path
ext = os.path.splitext(template_path)[1]
flash("Extension = " + ext )
if ext == '.json':
flash('var = .json')
template_filename = wget.download(template_location, out='app/storage/templates/json')
flash(template_filename)
elif ext in ('.yml', '.yaml'):
flash('var = .yaml')
return redirect(url_for('apps.index'))
return render_template('apps/new_template.html', form=form)
new_index.html:
{% extends 'layouts/base.html' %}
{% import 'macros/form_macros.html' as f %}
{% import 'macros/check_password.html' as check %}
{% block scripts %}
{% endblock %}
{% block content %}
<div class="ui stackable centered grid container">
<div class="twelve wide column">
<a class="ui basic compact button" href="{{ url_for('apps.index') }}">
<i class="caret left icon"></i>
Back to dashboard
</a>
<h2 class="ui header">
New App Template
<div class="sub header">Add a new app template</div>
{% set flashes = {
'error': get_flashed_messages(category_filter=['form-error']),
'warning': get_flashed_messages(category_filter=['form-check-email']),
'info': get_flashed_messages(category_filter=['form-info']),
'success': get_flashed_messages(category_filter=['form-success'])
} %}
{{ f.begin_form(form, flashes) }}
{{ f.render_form_field(form.template_url, extra_classes=novalidate) }}
{{ f.render_form_field(form.submit) }}
{{ f.end_form() }}
</h2>
</div>
</div>
{% endblock %}
No matter what I do I can't get the raise ValidationError('Invalid File Type') to work. I'm trying to just use https://test.com and it won't throw the validation error.
Please let me know if there's anything else I should attach to provide more information. Everything is importing correctly so I'm pretty sure it's not a dependency issue.
This is the boiler plate I'm using: https://github.com/hack4impact/flask-base
This resolved it. Thank you #ngShravil.py I needed to use a custom validator and separate it into it's own method (outside of the class).
def validate_filetype(form, field):
template_path = field.data
ext = os.path.splitext(template_path)[1]
if ext not in ('.json', '.yml', '.yaml'):
raise ValidationError('Invalid File Type')
class TemplateForm(FlaskForm):
template_url = URLField( 'Template URL', validators=[InputRequired(), URL(message='error'), validate_filetype])
submit = SubmitField('Add Template')
I am using class based views for my Django viewflow project. However I am getting the below error:
Reverse for 'index' not found. 'index' is not a valid view function or pattern name
Here are my classes:
rules/flows.py
class MyFlow(Flow):
"""
My App Flow
"""
process_class = models.MyProcess
start = flow.Start(
views.MyDetailView,
fields=["rule"],
task_title="New Task"
).Permission(
auto_create=True
).Next(this.end)
end = flow.End()
rules/urls.py
rule_urls = FlowViewSet(MyFlow).urls
urlpatterns = [
url(r'^rule/', include(rule_urls, namespace='rule')),
url(r'^(?P<pk>\d+)/mydetail/', MyDetailView.as_view(), {'flow_class': MyFlow, 'flow_task': MyFlow.start}, name='mydetail')
]
rules/models.py
class SubRules(models.Model):
rule_id = models.AutoField(primary_key=True)
src_id = models.IntegerField()
src_description = models.CharField(max_length=160, blank=True)
class Meta:
db_table = 'sub_rules'
class EntRules(models.Model):
rule = models.OneToOneField(SubRules, primary_key=True, on_delete=models.CASCADE)
entitlement_code = models.IntegerField()
rule_approved = models.SmallIntegerField()
rule_approved_datetime = models.DateTimeField(null=True)
class Meta:
db_table = 'ent_rules'
class MyProcess(Process):
entrule = models.ForeignKey(EntRules, blank=True, null=True, on_delete=models.CASCADE)
class Meta:
permissions = (
('approve_ent_rules','Can approve ent_rules')
)
rules/forms.py
class MyDetailForm(forms.ModelForm):
class Meta:
model = EntRules
fields = [
'rule',
'entitlement_code',
]
def __init__(self, *args, **kwargs):
pk = kwargs.pop('rule_id', None)
super(MyDetailForm, self).__init__(*args, **kwargs)
sub_rule = SubRules.objects.get(rule_id=pk)
self.fields['rule'].queryset = sub_rule.objects.filter(rule_id=pk)
rules/views/my_detail.py
class MyDetailView(StartFlowMixin, generic.UpdateView):
model = EntRules
template_name = 'rules/my_detail.html'
form_class = MyDetailForm
layout = Layout(
Fieldset('Rule Details',
Row('rule',),
Row('entitlement_code',),
)
)
def activation_done(self, form):
ent_rule = form.save()
self.activation.process.entrule = ent_rule
super(MyDetailView, self).activation_done(form)
def get_object(self):
return self.activation.process.entrule
def get_form_kwargs(self):
kwargs = super(MyDetailView, self).get_form_kwargs()
pk = int(self.kwargs['pk'])
kwargs['rule_id'] = pk
return kwargs
rules/templates/rules/my_detail.html
{% extends "base.html" %}
{% load material_form %}
{% block content %}
<form method="POST">
{% csrf_token %}
<div class="row">
{% form form=form %} {% endform %}
{{ activation.management_form }}
<div class="right">
<button type="submit" name="_start" class="btn">Submit</button>
</div>
</div>
</form>
{% endblock %}
Can you please advise what could be the issue here ?
I looked at Process Views outside viewflow.frontend
However how do I pass the sub_rule pk "^(?P\d+)" to startview as given in the URL like below ?
class MyFlow(Flow):
start = flow.Start(detail_view_class=MyDetailTaskView)
Please advise.
You can just pass a custom detail view as an argument for flow.Start http://docs.viewflow.io/viewflow_flow_nodes.html#viewflow.flow.StartFunction.detail_view
everyone!
Implementing the edit/add functions to the app, I found that Django ignores my clean_<field_name> method in some classes. When I tried to debug it I found out that the methods are not even called. Here are the relevant code snippets:
models.py
class StoredItem(models.Model):
item = models.ForeignKey(Item)
quantity = models.IntegerField(default=0)
class Meta:
ordering = ["item"]
def __str__(self):
return "{0} - {1}, {2} pcs.".format(self.item.serial, self.item.name, self.quantity)
def get_absolute_url(self):
return reverse('almacen:storeditem-detail', args=[str(self.id)])
forms.py
# Stored items related forms
class NewOrEditStoredItemForm(forms.Form):
item = forms.ModelChoiceField(queryset=StoredItem.objects.all())
quantity = forms.IntegerField()
def clean_item(self):
item = self.cleaned_data['item']
storeds = map(lambda x: x.item, StoredItem.objects.all())
if item in storeds:
raise ValidationError(_("This item has already been stored - you only can change its quantity in the stock!"))
def clean_quantity(self):
quantity = self.cleaned_data['quantity']
if quantity < 0:
raise ValidationError(_("Items quantity can not be negative!"))
return quantity
views.py
# Stored items related ---------------------------------------------------------------------
class CreateStoredItem(CreateView):
model = StoredItem
fields = '__all__'
class UpdateStoredItem(UpdateView):
model = StoredItem
fields = '__all__'
storeditem_form.html
{% extends 'base_template.html' %}
{% block header %}
<h2 id="center">Stocked item add/delete </h2>
{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit"/>
</form>
{% endblock %}
EDIT
Resolved by rewriting the classes into functions. For classes I was recommended to use validators, but anyway, functions worked as I need.
Resolved by rewriting the classes into functions. For classes I was recommended to use validators, but anyway, functions worked as I needed