Table in crispy-forms (django) using FormHelper - django

I want to show a table containing a set of model objects.
My model class:
from django.dbfrom django.db import models
from django.utils.translation import ugettext_lazy as _
class DamageKind(models.Model):
name = models.CharField(_('damage kind'), max_length=64)
regions = models.ManyToManyField(Region)
def __str__(self):
return self.name
class Meta:
verbose_name = _('damage kind')
verbose_name_plural = _('damage kinds')
my form class:
from django import forms
from crispy_forms.helper import FormHelper
from .models import DamageKind
class DamageKindList(forms.Form):
def __init__(self, *args, **kwargs):
self.damagekinds = kwargs.pop('damagekinds', [])
self.helper = FormHelper()
self.helper.form_method = 'post'
super().__init__(*args, **kwargs)
My base template base.html:
<!DOCTYPE html>
<html>
<body>
{% block list %}
{% endblock %}
</body>
</html>
my list_damagekinds.html:
{% extends "./base.html" %}
{% load crispy_forms_tags %}
{% block list %}
<form action="" method="post">
{% csrf_token %}
{{ damagekind_form }}
</form>
{% endblock %}
and my views.py:
def list_damagekinds(request):
damagekinds = DamageKind.objects.all()
return render(
request,
'damage/list_damagekinds.html',
{'damagekind_form': DamageKindList(damagekinds=damagekinds), }
)
so my question is how I can make a table containing all the names of the damagekinds by not beeing editable, so just showing these. And with using the crispy_forms FormHelper and not code it into the template.
Thanks in advance.

my list_damagekinds.html
{% extends "./base.html" %}
{% load crispy_forms_tags %}
{% block list %}
<form action="" method="post">
{% csrf_token %}
<table>
{% for obj in damagekind_form %}
<tr> {{ obj.name }} </tr>
{% endfor %}
</table>
</form>
{% endblock %}

Related

Reverse for 'allproduct' not found. 'allproduct' is not a valid view function or pattern name

urls.py
from django.urls import path
from . import views
app_name='shop'
urlpatterns = [
path('',views.allproduct,name='allproductcat'),
path('<slug:c_slug>/',views.allproduct,name='product_by_catagory')
]
views.py
from django.shortcuts import render, get_object_or_404
from . models import catagory,product
# Create your views here.
def allproduct(request,c_slug=None):
c_page=None
products=None
if c_slug!=None:
c_page=get_object_or_404(catagory,slug=c_slug)
products=product.objects.all().filter(catagory=c_page,available=True)
else:
products=product.objects.all().filter(available=True)
return render(request,'catagory.html',{'catagory':c_page,'products':products})
model.py
from django.db import models
# Create your models here.
from django.urls import reverse
class catagory(models.Model):
name=models.CharField(max_length=250,unique=True)
slug=models.SlugField(max_length=250,unique=True)
description=models.TextField(blank=True)
image=models.ImageField(upload_to='catagory',blank=True)
class Meta:
ordering=('name',)
verbose_name='catagory'
verbose_name_plural='catagories'
def __str__(self):
return '{}'.format(self.name)
def get_url(self):
return reverse('shop:product_by_catagory',args=(self.slug,))
class product(models.Model):
name = models.CharField(max_length=250, unique=True)
slug = models.SlugField(max_length=250, unique=True)
description = models.TextField(blank=True)
image = models.ImageField(upload_to='product', blank=True)
price=models.DecimalField(max_digits=10,decimal_places=2)
stock=models.IntegerField()
available=models.BooleanField()
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now=True)
catagory=models.ForeignKey(catagory,on_delete=models.CASCADE)
class Meta:
ordering = ('name',)
verbose_name = 'product'
verbose_name_plural = 'products'
def __str__(self):
return '{}'.format(self.name)
catagory. html
{% extends 'base.html' %}
{% load static %}
{% block metadiscription %}
{% if catagory %}
{{catagory.discription}}
{% else %}
welcome
{% endif %}
{% endblock %}
{% block title %}
{% if catagory %}
{{catagory.name}}--ABC store
{% else %}
see our new collection
{% endif %}
{% endblock %}
{% block content %}
{% if catagory %}
<div>
<div>
OUR PRODUCT COLLECTION
</div>
</div>
{% endif %}
<div>
{% if catagory %}
<img src="{{catagory.img.url}}" alt="{{catagory.name}}">
</div>
<br>
<div>
<h1>
{{catagory.name}}
</h1>
<p>
{{catagory.discription}}
</p>
</div>
{% else %}
<div>
<img src="{% static 'img/banner.png' %}">
</div>
<br>
<div>
<h1>OUR PRODUCT COLLECTION</h1>
<p>INDIA IS MY COUNTRY I LOVE MY COUNTRY</p>
</div>
{% endif %}
<div>
<div>
{% for product in products %}
<div>
<div>
<div>
<h4>{{product.name}}</h4>
<p>{{product.price}}</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
context_processors.py
from . models import catagory
def menu_link(request):
link=catagory.objects.all()
return dict(links=link)
i can't load the catagory page using the slug address,that is dress, when i enter that it is showing there is no revrse for allproducts, and when i open the website there is no image in image position.as i have uploaded the image file in the admin pannel
The name=… of your path is allproductcat, not allproduct, you thus should implement a link with:
OUR PRODUCT COLLECTION

Django simple search with Class based views and forms.py

I have been trying to do a variation of what Williams Vincent did on this page: https://learndjango.com/tutorials/django-search-tutorial .
I am using Django 3.2 so if there are modifications, I need to make I have not identified them. I am having some troubles.
This what I made which worked just fine.
my_search.html:
{% extends "base.html" %}
{% block body %}
{% for city in object_list %}
<li>
{{city.name}}   {{city.city_no}}
</li>
{% endfor %}
{% endblock %}
views.py:
from django.views.generic import ListView
from .models import City
class SearchResutlsView(ListView): # test version
model = City
template_name = "search_results.html"
def get_queryset(self):
return City.objects.filter(name__icontains='Boston')
Now it is time to add forms.py, but when I made the below changes to the code it does not work. What am I missing? There are no errors displayed. I get a blank html.
{% extends "base.html" %}
{% block body %}
<form class="d-flex" method='get' action="{% url 'city:search_results' %}">
{{ form }}
<button class="btn btn-outline-success" type="submit" value="qu">Search Name</button>
</form>
{% for city in city_list %}
<li>
{{city.name}}   {{city.city_no}}
</li>
{% endfor %}
{% endblock %}
forms.py
from django import forms
class SearchForm(forms.Form):
q = forms.CharField(label='Search label', max_length=50, strip=True)
views.py
from django.views.generic import FormView, ListView
from .models import City
class SearchResutlsView(FormView):
model = City
form_class = SearchForm
template_name = "city/search_results.html"
def get_queryset(self):
query = self.request.Get.get("q")
if query:
city_list = City.objects.filter(name__icontains=query)
else:
city_list = City.objects.none()
return city_list
First, Your method should be POST not get.
Second, you need to add CSRF token.
something like that:
{% extends "base.html" %}
{% block body %}
<form class="d-flex" method='post' action="{% url 'city:search_results' %}">
{% csrf_token %}
{{ form }}
<button class="btn btn-outline-success" type="submit" value="qu">Search Name</button>
</form>
{% for city in city_list %}
<li>
{{city.name}}   {{city.city_no}}
</li>
{% endfor %}
{% endblock %}
and in views.py
query = self.request.POST.get("q")

how to implement search that can access complete database in Django

My views.py
class SearchView(TemplateView):
template_name = 'search.html'
def get(self, request, *args, **kwargs):
q = request.GET.get('q', '')
self.results = Item.objects.filter(title__icontains=q)
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
return super().get_context_data(results=self.results, **kwargs)
My urls.py
url(r'^search/$', SearchView.as_view(), name='search')
My search.html
{% extends 'base.html' %} {% load static %} {% block content %}
<body>
<h1>Search Result</h1>
<ul>
{% for item in q %}
<li>
{{ q.title }}, {{ q.price }}
</li>
{% endfor %}
</ul>
</body>
{% endblock%}}
My nav.html
<form method="GET" action="{% url 'core:search' %}">
This is the code that i used but due to some missing or error in this above code i can't get any data if i make any search in my website, Can some one please tell me what is the mistake i have done.
Thank you.
My Models.py
class Item(models.Model):
title = models.CharField(max_length=50)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
model_no = models.CharField(max_length=100)
try this:
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['results'] = self.results
return context
in html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<body>
<h1>Search Result</h1>
<ul>
{% for item in results %}
<li>
{{ item.title }}, {{ item.price }}
</li>
{% endfor %}
</ul>
</body>
{% endblock%}}

django-filter no matches

I couldn't find it in documentation so Im asking here. I've created filter with django-filter and it works properly, but if someone will select filters that none object has, then user will get empty page. I would like to add simple paragraph
None criteria matches
if filtered object don't exist.
I've tried with template tags like
{% if obj in filter.qs != none %}
{% endif %}
But it doesn't work. Does someone know how to make it?
filters.py
import django_filters
from .models import Company, COMPANY_TECHNOLOGIES
from django_filters import ChoiceFilter
class CompanyFilter(django_filters.FilterSet):
class Meta:
model = Company
fields = ['type', 'city', 'students']
def __init__(self, *args, **kwargs):
super(CompanyFilter, self).__init__(*args, **kwargs)
self.filters['type'].extra.update(
{'empty_label': 'All'})
self.filters['city'].extra.update(
{'empty_label': 'All'})
self.filters['students'].extra.update(
{'empty_label': 'All'})
comp_list.html
{% extends 'company/base.html' %}
{% block content %}
<div id="filter">
<form action="" method="get" id="submit">
{{ filter.form.as_p }}
<input type="submit"/>
</form>
{% for obj in filter.qs %}
{{ obj.name }}
<p>Image {% if obj.image != None %}
<img src="{{ obj.image.url }}">
{% endif%}</p>
<p>Icon {% if obj.icon != None %}
<img src="{{ obj.icon.url }}" width="30" height="30">
{% endif%}</p>
<br> Type: {{ obj.type }} City: {{ obj.city }} Stack: {{ obj.stack }}
{% if obj not in filter.qs %}
<p>no matches</p>
{% endif %}
{% endfor %}
{% endblock %}
</div>
views.py
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from .models import Company
from .filters import CompanyFilter
from rest_framework import viewsets
from .serializers import CompanySerializer
# Create your views here.
def comp_list(request):
f = CompanyFilter(request.GET, queryset=Company.objects.all())
return render(request, 'company/comp_list.html', {'filter': f})
##def brands(request, slug):
##brands = Company.objects.all()
##return render(request, 'company/comp_view.html', {'brands': brands})
def brands(request, pk):
brand = get_object_or_404(Company, pk=pk)
return render(request, 'company/comp_view.html', {'brand': brand})
#rest api
class CompanyViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Company.objects.all()
serializer_class = CompanySerializer
Sorry for taking your time, I should go straight away to django docs and use built in template tag
{% empty %}
I've just added this into my comp_list.html like below
{% extends 'company/base.html' %}
{% block content %}
<div id="filter">
<form action="" method="get" id="submit">
{{ filter.form.as_p }}
<input type="submit"/>
</form>
{% for obj in filter.qs %}
{% if obj in filter.qs %}
{{ obj.name }}
<p>Image {% if obj.image != None %}
<img src="{{ obj.image.url }}">
{% endif%}</p>
<p>Icon {% if obj.icon != None %}
<img src="{{ obj.icon.url }}" width="30" height="30">
{% endif%}</p>
<br> Type: {{ obj.type }} City: {{ obj.city }} Stack: {{ obj.stack }}
{% endif %}
{% empty %}
<p>no matches</p>
{% endfor %}
{% endblock %}</div>
And now everything works as it should, if user will try to filter object with criterias that dont match he will get response as paragraph <p> No Matches </p>

Bootstrap3 inline forms in django-crispy-forms not showing form errors

I am using django-crispy-forms to render a Bootstrap3 inline form (code shown below), but the errors upon form submission (like skipping required fields) are not being shown. They do in normal and horizontal form layouts.
Could someone please suggest the possible reason(s)?
Models.py
class Person(models.Model):
name = models.CharField(max_length=500)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
email = models.EmailField(blank=True)
Forms.py
class EntryForm(forms.ModelForm):
class Meta:
model = Person
def __init__(self, *args, **kwargs):
super(EntryForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_class = 'form-inline'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.layout.append(ButtonHolder(
Submit('save', 'Save', css_class='btn-primary btn-hg')
)
)
I am using {% load crispy_forms_tags %} and {% crispy form %} in my template.
The reason is that the inline_field.html template that you use, doesn't have a code to display errors.
Please compare normal bootstrap3/field.html to the inline version. You will notice that the
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
is missing in the latter. After you change the inline to something like below you'll have error messages back.
{% load crispy_forms_field %}
{% if field.is_hidden %}
{{ field }}
{% else %}
{% if field|is_checkbox %}
<div id="div_{{ field.auto_id }}" class="checkbox">
<label for="{{ field.id_for_label }}" class="{% if field.field.required %} requiredField{% endif %}">
{% crispy_field field 'class' 'checkbox' %}
{{ field.label|safe }}
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
</label>
</div>
{% else %}
<div id="div_{{ field.auto_id }}" class="form-group">
<label for="{{ field.id_for_label }}" class="sr-only{% if field.field.required %} requiredField{% endif %}">
{{ field.label|safe }}
</label>
{% crispy_field field 'placeholder' field.label %}
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
</div>
{% endif %}
{% endif %}
Of course error messages are quite ugly (as they come from normal version) so you probably have to create inline version of the bootstrap3/layout/help_text_and_errors.html. Also some error css classes might be needed - see the field.html.
Below is the current configuration for my projects. I think it might work for you too.
#forms.py
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Div
from crispy_forms.bootstrap import FormActions
from myapp.models import Person
class EntryForm(forms.ModelForm):
class Meta:
model = Person
def __init__(self, *args, **kwargs):
super(EntryForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-entryform'
self.helper.form_class = 'form-inline'
self.helper.form.method = 'post'
self.helper.form.action = ''
self.helper.layout = Layout(
Div('name','email'),
Div('country','city'),
FormActions(Submit('save', 'Save', css_class='btn-primary btn-hg')
)
)