Django - display items from a collection inside a template - django

I am building an e-commerce store with Django, and I have added some products to a collection. I want to be able to display each collection separately with the products that have been added to it.
Here is my models.py code
#Collections model Fields
class Collections(models.Model):
title = models.CharField(max_length=100)
products = models.ManyToManyField(Product)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('collections', kwargs={'slug': self.slug})
Here is my Views.py:
def collectionsPage(request, slug):
collections = Collections.objects.get(slug=slug)
products = Product.objects.all()
context = {"products": products, "collections":collections,}
return render(request, "collections.html", context)
Here is my urls.py:
urlpatterns = [
path('collections/<slug:slug>', views.collectionsPage, name="collections"),
]
Here is my HTML Code:
{% for collection in collections %}
<form action="{{collection.products.get_absolute_url}}" method="post">
{% csrf_token %}
<button class="polaroid" style="background-color:white; border:none">
<div>
<img src="{{collection.products.imageURL}}" alt="iPhone image">
<h3 class="container">{{collection.products.title}}</h3>
<h4 class="container">{{collection.products.price}}</h4>
</div>
</button>
</form>
{% endfor %}

I have been able to solve the problem as follows:
#Nothing to change in Models.py
#Views.py
def collectionsPage(request, slug):
collections = Collections.objects.get(slug=slug).products.all() ###
data = cartData(request)
cartItems = data["cartItems"]
context = {"collections":collections, "cartItems":cartItems}
return render(request, "collections.html", context)
#HTML code:
{% for product in collections %}
<form action="{{product.get_absolute_url}}" method="post">
{% csrf_token %}
<button class="polaroid" style="background-color:white; border:none">
<div>
<img src="{{product.imageURL}}" alt="iPhone image">
<h3 class="container">{{product.title}}</h3>
<h4 class="container">{{product.price}}</h4>
</div>
</button>
</form>
{% endfor %}

Related

Show django form in a designed page

How are you?
I m totally new in Django.I designed a page and I wanted to show a django form(edit or create) in a well designed HTML page. but i do not know how.
This is my owner method:
class OwnerUpdateView(LoginRequiredMixin, UpdateView):
"""
queryset to the requesting user.
"""
def get_queryset(self):
print('update get_queryset called')
""" Limit a User to only modifying their own data. """
qs = super(OwnerUpdateView, self).get_queryset()
return qs.filter(user=self.request.user)
class OwnerCreateView(LoginRequiredMixin, CreateView):
"""
Sub-class of the CreateView to automatically pass the Request to the Form
and add the owner to the saved object.
"""
# Saves the form instance, sets the current object for the view, and redirects to get_success_url().
def form_valid(self, form):
print('form_valid called')
object = form.save(commit=False)
object.user = self.request.user
object.save()
return super(OwnerCreateView, self).form_valid(form)
This is my views.py
class TaskUpdateView(OwnerUpdateView):
model = Task
fields = ["title", "text", "endDate"]
class TaskCreateView(OwnerCreateView):
model = Task
fields = ["title","text","status","endDate"]
This is my urls.py:
app_name='task'
urlpatterns = [
path('', views.TaskListView.as_view(), name='all'),
path('task/<int:pk>/', views.TaskDetailView.as_view(), name='detail'),
path('task/create', views.TaskCreateView.as_view(success_url=reverse_lazy('task:all')), name='task_create'),
path('task/update/<int:pk>', views.TaskUpdateView.as_view(success_url=reverse_lazy('task:all')),
name='task_update'),
path('task/delete/<int:pk>', views.TaskDeleteView.as_view(success_url=reverse_lazy('task:all')),
name='task_delete'),
path("accounts/login/", views.login, name='login'),
path("accounts/logout/", views.logout, name='logout'),
]
And this is the models.py:
class Task(models.Model):
title=models.CharField(max_length=250)
text=models.TextField()
user=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False)
status=models.ForeignKey('Status',on_delete=models.SET_NULL,null=True)
startDate=models.DateTimeField(auto_now_add=True)
endDate=models.DateField(null=True)
def __str__(self):
return self.title
class Status(models.Model):
name=models.CharField(max_length=250)
def __str__(self):
return self.name
And this is where these both function work:
{%extends 'base.html'%}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<table>{{ form.as_table }}</table>
<input type="submit" value="Submit">
{# <input type="submit" onclick="window.location='{% url 'project:all' %}' ; return false;" value="Cancel">#}
</form>
{% endblock %}
How can i separate each element of this form and put it in a better designed page?
Thanks
There are two ways:
Option 1:
Loop over the form fields and render them individually:
{% for field in form %}
<div class="form-group">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<span class="form-text">{{ field.help_text|safe }}</span>
{% endif %}
</div>
{% endfor %}
See docs for more.
Option 2:
You can manually create form inputs and give them the correct field name attribute. This gives you more control but also requires more work:
<div class="form-group"
<input
type="text"
name="title"
value="{{ form.title.value }}"
class="form-control {% if form.title.errors %}is-invalid{% endif %}"
>
{% if form.title.help_text%}
<span class="form-text">{{ form.title.help_text|safe }}</span>
{% endif %}
<div class="invalid-feedback">{{ form.title.errors }}</div>
</div>
<!-- now do the same for other fields -->

Django-Why doesn't the likes system work?

Why is the likes system not working? Аfter clicking on the "like" button I get an error.
class ImageDetail(DetailView):
model=Image
template_name='images/image/detail.html'
context_object_name='image'
def get_queryset(self):
return Image.objects.filter(id=self.kwargs.get('id'),slug=self.kwargs['slug'])
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
likes_connected=get_object_or_404(Image, id=self.kwargs['id'],slug=self.kwargs['slug'])
liked=False
if likes_connected.users_like.filter(id=self.request.user.id).exists():
liked=True
data['number_of_likes']=likes_connected.number_of_likes()
data['post_is_liked']=liked
return data
def ImagePostLike(request,id,slug):
image=get_object_or_404(Image, id=request.POST.get('image_id'), slug=request.POST.get('image_slug'))
if image.users_like.filter(id=request.user.id).exists():
image.users_like.remove(request.user)
else:
image.users_like.add(request.user)
return HttpResponseRedirect(reverse('image_detail', args=[self.id, self.slug]))
Why is the likes system not working? Аfter clicking on the "like" button I get an error.
urls.py
from django.urls import path,include
from . import views
app_name = 'images'
urlpatterns = [
path('create/', views.image_create, name='create'),
path('detail/<int:id>/<slug:slug>/', views.ImageDetail.as_view(), name='image_detail'),
path('image_like/<int:id>/<slug:slug>/', views.ImagePostLike, name='image_like'),
]
Why is the likes system not working? Аfter clicking on the "like" button I get an error.
detail.html
{% extends 'base.html' %}
{% load thumbnail %}
{% block title %}{{image.title}}{% endblock title %}
{% block content %}
<h1>{{ image.title }}</h1>
<img src="{{ image.url }}" class="image-detail">
{% if user.is_authenticated %}
<form action="{% url 'images:image_like' image.id image.slug%}">
{% csrf_token %}
{% if post_is_liked %}
<button type="submit" name="image_id" value="{{image.id}}" class="btn btn-info">Unlike</button>
{% else %}
<button type="submit" name="image_id" value="{{image.id}}" class="btn btn-info">Like</button>
{% endif %}
</form>
{% else %}
<a class="btn btn-outline-info" href="{% url 'login' %}?next={{request.path}}">>Log in to like this article!</a><br>
{% endif %}
<strong class="text-secondary">{{ number_of_likes }} Like</strong>
{% endblock content %}
models.py
# Create your models here.
class Image(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='images_created', on_delete=models.CASCADE)
title=models.CharField(max_length=50)
slug=models.SlugField(max_length=200, blank=True)
url=models.URLField()
image=models.ImageField(upload_to='images/%Y/%m/%d/')
description = models.TextField(blank=True)
created = models.DateField(auto_now_add=True,
db_index=True)
users_like=models.ManyToManyField(User,
related_name='image_like')
def __str__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('images:detail', args=[self.id, self.slug])
def number_of_likes(self):
return self.users_like.count()
As I understand it, the problem lies in the slug.

Django: How to add another form to my homepage when the homepage already has a template

On my homepage(http://127.0.0.1:8000/) I created a template and its function in views.py and the URL of the homepage directs to it, however, there is a form that I want also to show on the homepage.
views.py:
def index(request):
template = loader.get_template('tracker/index.html')
hours_grouped_project =
LogHours.objects.all().order_by('proj_assignment__project__name', 'day')
context = {
'hours_grouped_project':hours_grouped_project,
}
return HttpResponse(template.render(context,request))
def form(request):
if request.method== 'POST':
form = LogHoursForm(request.POST)
if form.is_valid():
day = form.cleaned_data['day']
hours = form.cleaned_data['hours']
developer = form.cleaned_data['developer']
project = form.cleaned_data['project']
proj_assignment=ProjectAssignment.objects.create(
developer=developer,project=project)
LogHours.objects.create(day=day,hours=hours,
proj_assignment= proj_assignment)
else:
form = LogHoursForm()
return render(request, 'tracker/form.html',{'form': form})
the index is for the template displayed on the homepage, and the form is for the form.py.
form.py
class LogHoursForm(forms.Form):
day = forms.DateField()
hours = forms.DecimalField(max_digits=10, decimal_places=2)
developer = forms.ModelChoiceField(queryset=Developer.objects.all())
project = forms.ModelChoiceField(queryset=Project.objects.all())
and here is the URL: urlpatterns = [
path('', views.index, name='index'),]
the form.html is
<h1> LogHours Form</h1>
<hr>
<form action="" method="post">
{% csrf_token %}
{{form}}
<input type="submit">
</form>
and the index.html:
<ul>
<b>Logged hours grouped by project:</b>
{% for obj in hours_grouped_project %}
<li>
{% ifchanged obj.proj_assignment %}
<b>Project name:</b>
{{obj.proj_assignment|linebreaks}}
{% endifchanged %}
<i>Sorted Date: </i>
{{obj}}
</li>
{% endfor %}
</ul>
In your function index(request):
def index(request):
if request.method=='POST':
form(request)
else:
template = loader.get_template('tracker/index.html')
hours_grouped_project = LogHours.objects.all().order_by('proj_assignment__project__name', 'day')
form = LogHoursForm()
context = {
'hours_grouped_project': hours_grouped_project,
'form': form,
}
return HttpResponse(template.render(context, request))
Then, you can render the form from your index.html like this.
...
<form action="" method="post">
{% csrf_token %}
{{form}}
<input type="submit">
...

Django Form after redirect is empty after successfully submitting a form

I have a template called courses for the url http://127.0.0.1:8000/gradebook/courses/. This template lists existing Course objects loads the CourseForm form. The form successfully creates new objects.
If I go to the addassessment template with url http://127.0.0.1:8000/gradebook/addassessment/7/, it correctly loads the AssessmentForm. I want to submit this form and then return to the previous courses template. The AssessmentForm submits and the object is saved, but when it redirects back to the courses template, the CourseForm does not load. The courses template loads, the expect html loads correctly other than the form fields. I notice that the url for this page is still http://127.0.0.1:8000/gradebook/addassessment/7/ and not ../gradebook/courses/.
app_name = 'gradebook'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('signup/', SignUpView.as_view(), name='signup'),
path('courses/', views.courses, name='courses'),
path('classroom/', views.classroom, name='classroom'),
path('objective/<int:course_id>/', views.addobjective, name='addobjective'),
path('addassessment/<int:course_id>/', views.addassessment, name='addassessment'),
]
#urls.py project
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('gradebook/', include('gradebook.urls')),
path('gradebook/', include('django.contrib.auth.urls')),
]
#models.py
class Course(models.Model):
course_name = models.CharField(max_length=10)
class Classroom(models.Model):
classroom_name = models.CharField(max_length=10)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
class Assessment(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
assessment_name = models.CharField(max_length=10)
objectives = models.ManyToManyField('Objective')
class Objective(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
objective_name = models.CharField(max_length=10)
objective_description = models.CharField(max_length=30)
#views.py
def courses(request):
course_list = Course.objects.order_by('course_name')
context = {'course_list': course_list}
if request.method == 'POST':
details = CourseForm(request.POST)
if details.is_valid():
course = details.save(commit=False)
course.save()
form = CourseForm(None)
context['form'] = form
return render(request, "gradebook/courses.html", context)
else:
context['form'] = details
return render(request, "gradebook/courses.html", context)
else:
form = CourseForm(None)
context['form'] = form
return render(request, "gradebook/courses.html", context)
def addassessment(request, course_id):
course_list = Course.objects.order_by('course_name')
this_course = Course.objects.get(id=course_id)
objectives = Objective.objects.filter(course=this_course).order_by('objective_name')
context = {'this_course': this_course}
context['objectives'] = objectives
context['course_list'] = course_list
if request.method == 'POST':
form = AssessmentForm(request.POST)
if form.is_valid():
assess = form.save(commit=False)
# save the course that the objective belongs to
assess.course = this_course
assess.save()
form.save_m2m()
return render(request, "gradebook/courses.html", context)
else:
context['form'] = form
return render(request, "gradebook/addassessment.html", context)
else:
form = AssessmentForm(None)
form.fields["objectives"].queryset = Objective.objects.filter(course=this_course)
context['form'] = form
return render(request, "gradebook/addassessment.html", context)
#forms.py
class AssessmentForm(ModelForm):
class Meta:
model = Assessment
fields = ('assessment_name', 'objectives',)
class CourseForm(ModelForm):
class Meta:
model = Course
fields = ["course_name"]
def clean(self):
super(CourseForm, self).clean()
course_name = self.cleaned_data.get('course_name')
if course_name and Course.objects.filter(course_name__iexact=course_name).exists():
self.add_error(
'course_name', 'A course with that course name already exists.')
if len(course_name) > 10:
self.add_error(
'Your course name cannot be longer than 10 characters')
return self.cleaned_data
#courses template: course.html
<div class="container">
<div class="row">
<div class="twelve columns">
<h1>Courses</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
{% if course_list %}
<ul>
{% for course in course_list %}
<li><div class = "row">
<div class = "col-md-3">
{{ course.course_name }}
</div>
</div></li>
{% endfor %}
</ul>
{% else %}
<p>No Courses are available.</p>
{% endif %}
</div>
</div>
<div class="row">
<div class="col-md-3">
<p>Create a new course</p>
</div>
<div class="col-md-3">
<form action="{% url 'gradebook:courses' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<div class="form-group">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</form>
</div>
</div>
</div>
#addassessment template: addassessment.html
<div class="container">
<div class="row">
<div class="twelve columns">
<h1>{{ this_course }}</h1>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p>Add an assessment to your class</p>
</div>
<div class="col-md-3">
<div class="form-group">
<form action="" method="post">
{% csrf_token %} {{ form|crispy }}
<div class="form-group">
<button type="submit" class="btn btn-secondary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
There are no error messages.
When you submit your assessment form at http://127.0.0.1:8000/gradebook/addassessment/7/ your sending a post request back to your addassessment view function to process your form which you know. The url will still be the same as a result, which is what you are seeing.
I would suggest against returning the courses template if your assessment form is valid in the way you have written
#inside addassessment view function
return render(request, "gradebook/courses.html", context)
If you are just wanting to redirect to the courses view upon successfully saving the assessment form I suggest using the redirect shortcut.
# add to your import
from django.shortcuts import render, redirect
#inside addassessment view function - replacing "return render(request, "gradebook/courses.html", context)"
return redirect(courses)
This will send your request object to the courses view function. The redirect will use a GET action instead of a post so you'll just see what you would normally at http://127.0.0.1:8000/gradebook/courses/. This will also be the url!

Using modelformset_factory in a Django Class Based View

I am building a view that will let me update multiple fields on multiple objects at the same time. I'm doing this using ModelFormSet & modelformset_factory.
The template will be a table of forms with the object name to the left of the fields (see image below).
I found this example, but I am stuck on how to implement the class based view & template.
My Formset
class BaseFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super(BaseFormSet, self).__init__(*args, **kwargs)
self.queryset = Reference.objects.filter(
start__isnull=True)
ReferenceFormSet = modelformset_factory(
Reference,
fields=('start', 'end'),
formset=BaseFormSet,
extra=0)
My View
class ReferenceFormSetView(LoginRequiredMixin, SuperuserRequiredMixin, FormView):
model = Reference
form_class = ReferenceFormSet
template_name = "references/references_form.html"
def form_valid(self, form):
for sub_form in form:
if sub_form.has_changed():
sub_form.save()
return super(ReferenceFormSetView, self).form_valid(form)
My Template
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<h1>{{ headline }}</h1>
<div class="row">
<form action="" method="post">
{% crispy form %}
<div class="">
<input type="submit" value="Submit" />
</div>
</form>
</div>
</div>
{% endblock content %}
Questions
The view seems odd with the Formset in the form_class. Is there a better way to handle this?
How can I access the instance name to display in the form?
I found a solution using a package called django-extra-views.
There is a class called ModelFormSetView which does exactly what I wanted. Here is my implementation (simplified) for others to use -
My View
class ReferenceFormSetView(ModelFormSetView):
model = Reference
template_name = "references/references_form.html"
fields = ['start', 'end']
extra = 0
def get_queryset(self):
return self.model.objects.all()
def get_success_url(self):
return reverse('references:formset')
def formset_valid(self, formset):
"""
If the formset is valid redirect to the supplied URL
"""
messages.success(self.request, "Updated")
return HttpResponseRedirect(self.get_success_url())
def formset_invalid(self, formset):
"""
If the formset is invalid, re-render the context data with the
data-filled formset and errors.
"""
messages.error(self.request, "Error dummy")
return self.render_to_response(self.get_context_data(formset=formset))
My Template
<form class="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
<div class="">
{% for field in form %}
{{ field }}
{% endfor %}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Save</button>
</form>