Hello! I’m very new to Django and just started learning recently. I’m having an issue with my website, where a submitted form does not show properly on the screen. However, when I login to the admin site, I can see the submissions there. I dug through my code for hours but I have no idea which part is off. Is it my template? My views? Or none of that?
To give an idea of the issue, here is how to reproduce the problem. Go to this link here, login with the following details:
User: test
Password: helpme!!
Add a breed, submit, and then notice that when you view breeds, it is not updated on the website (although I can see it on the admin site).
I have attached my models, urls and views code here for reference.
from django.db import models
from django.core.validators import MinLengthValidator
#https://docs.djangoproject.com/en/3.2/ref/models/instances/#django.db.models.Model
#define a new class, "Breed". Has only 1 field.
class Breed(models.Model):
#creating "name" field. https://docs.djangoproject.com/en/3.2/topics/db/models/#fields
#https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.Field
name = models.CharField(
max_length=200,
help_text='Enter a cat breed (Maine Coon)',
#set validator with min length of 2
validators=[MinLengthValidator(2, "Breed must be greater than 1 character")]
)
#converting model to a string
def __str__(self):
return self.name
#define a new class, "Cat". Has 4 fields.
class Cat(models.Model):
#creating "nickname" field.
nickname = models.CharField(
max_length=200,
validators=[MinLengthValidator(2, "Nickname must be greater than 1 character")]
)
#creating "weight" field. set floating point
weight = models.FloatField()
#creating "food" field. table to save food comments
food = models.CharField(max_length=300)
#creating "breed" field.if user deletes a breed, the corresponding cat entries would be deleted
#https://www.techopedia.com/definition/7272/foreign-key
breed = models.ForeignKey('Breed', on_delete=models.CASCADE, null=False)
# Shows up in the admin list
def __str__(self):
return self.nickname
from django.urls import path
# from current directory, import views.py
from . import views
# https://docs.djangoproject.com/en/3.0/topics/http/urls/
#state the app name
app_name = 'cats'
urlpatterns = [
#add routes for create, update and delete pages for the cat
#https://docs.djangoproject.com/en/3.2/topics/class-based-views/
path('', views.CatView.as_view(), name='all'),
path('main/create/', views.CatCreate.as_view(), name='cat_create'),
#pk refers to primary key, both update & delete requires pk
path('main/<int:pk>/update/', views.CatUpdate.as_view(), name='cat_update'),
path('main/<int:pk>/delete/', views.CatDelete.as_view(), name='cat_delete'),
#add routes for create, update and delete pages for the breeds
path('lookup/', views.BreedView.as_view(), name='breed_list'),
path('lookup/create/', views.BreedCreate.as_view(), name='breed_create'),
#set path for update and delete for make
path('lookup/<int:pk>/update/', views.BreedUpdate.as_view(), name='breed_update'),
path('lookup/<int:pk>/delete/', views.BreedDelete.as_view(), name='breed_delete'),
]
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import View
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from cats.models import Cat, Breed
# create class "CatView", with authentication required
class CatView(LoginRequiredMixin, View):
def get(self, request):
breedcount = Breed.objects.all().count()
catlist = Cat.objects.all()
#define context
contxt = {'Breed_count': breedcount, 'cat_list': catlist}
return render(request, 'cats/cat_list.html', contxt)
# create class "BreedView", with authentication required
class BreedView(LoginRequiredMixin, View):
def get(self, request):
breedlist = Breed.objects.all()
contxt = {'Breed_list': breedlist}
return render(request, 'cats/breed_list.html', contxt)
# Create, update and delete Breed
class BreedCreate(LoginRequiredMixin, CreateView):
model = Breed
fields = '__all__'
#if successful, go to the main cats url. Refer to urls.py
success_url = reverse_lazy('cats:all')
class BreedUpdate(LoginRequiredMixin, UpdateView):
model = Breed
fields = '__all__'
success_url = reverse_lazy('cats:all')
class BreedDelete(LoginRequiredMixin, DeleteView):
model = Breed
fields = '__all__'
success_url = reverse_lazy('cats:all')
# Create, update and delete Cat
class CatCreate(LoginRequiredMixin, CreateView):
model = Cat
fields = '__all__'
success_url = reverse_lazy('cats:all')
class CatUpdate(LoginRequiredMixin, UpdateView):
model = Cat
fields = '__all__'
success_url = reverse_lazy('cats:all')
class CatDelete(LoginRequiredMixin, DeleteView):
model = Cat
fields = '__all__'
success_url = reverse_lazy('cats:all')
Any guidance at all would be very much appreciated!
Edit: Not sure if it’s useful but here are the html files in my template
and the files in my main cats folder are
admin.py
apps.py
forms.py
models.py
tests.py
urls.py
views.py
init.py