I successfully created the workflow of creating/viewing/editing a Thing and associating it with a user. However, I’d like to (advance it?) create a new model relationship and am struggling. Ideally the user creates a profile that identifies the Shelter they work for. And then the user creates a Thing, which corresponds with the Shelter–because I don’t want the user to have to fill in shelter fields every time they create a Thing.
So I created a second model and replicated the create/view/edit Thing templates and views. My model set up is below. But when I try to link the profile and thing in the same index I get the following error. What all am I missing here?
NoReverseMatch at /
Reverse for 'shelter_detail' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['profile\\/(?P<slug>[^/]+)\\/$']
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Shelter(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.SlugField(unique=True)
website = models.URLField(max_length=200, default='')
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.name
class Thing(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.SlugField(unique=True)
shelter = models.ManyToManyField(Shelter)
def __str__(self):
return self.name
from django.contrib.auth.views import (
password_reset,
password_reset_done,
password_reset_confirm,
password_reset_complete,
)
from django.contrib import admin
from django.urls import include, path
from django.views.generic import TemplateView
from collection import views
from collection.backends import MyRegistrationView
urlpatterns = [
path('', views.index, name='home'),
path('admin/', admin.site.urls),
path('profile/<slug>/', views.shelter_detail, name='shelter_detail'),
path('profile/<slug>/edit/', views.edit_shelter, name='edit_shelter'),
path('things/<slug>/', views.thing_detail, name='thing_detail'),
path('things/<slug>/edit/', views.edit_thing, name='edit_thing'),
# the new password reset URLs
path('accounts/', include('registration.backends.simple.urls')),
path('accounts/create_thing/', views.create_thing, name='create_thing'),
path('accounts/password/reset/', password_reset,
{'template_name': 'registration/password_reset_form.html'}, name="password_reset"),
path('accounts/password/reset/done/', password_reset_done,
{'template_name': 'registration/password_reset_done.html'}, name="password_reset_done"),
path('accounts/password/reset/<uidb64>/<token>/', password_reset_confirm,
{'template_name': 'registration/password_reset_confirm.html'}, name="password_reset_confirm"),
path('accounts/password/done/', password_reset_complete,
{'template_name': 'registration/password_reset_complete.html'},
name="password_reset_complete"),
path('accounts/register/', MyRegistrationView.as_view(), name='registration_register'),
]