I am learning Django by building an application, called TravelBuddies. It will allow travelers to plan their trip and keep associated travel items (such as bookings, tickets, copy of passport, insurance information, etc), as well as create alerts for daily activities. The application will also able to update local information such as weather or daily news to the traveler. Travelers can also share the travel information with someone or have someone to collaborate with them to plan for the trip.
I am facing a problem. I have a form like this:
When I click on the Submit button, I am supposed to be redirected to http://127.0.0.1:8000/triplist/johor-bahru/. Instead, I get this error:
NoReverseMatch at /addactivity/
Reverse for 'activity' with no arguments not found. 1 pattern(s) tried: ['triplist/(?P<slug>[-a-zA-Z0-9_]+)/$']
Request Method: POST
Request URL: http://127.0.0.1:8000/addactivity/
Django Version: 3.0
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'activity' with no arguments not found. 1 pattern(s) tried: ['triplist/(?P<slug>[-a-zA-Z0-9_]+)/$']
Here are my codes in models.py
inside Trips folder:
from django.contrib.auth.models import User
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Trip(models.Model):
trip_name = models.CharField(max_length=100)
date = models.DateField()
planner_name = models.CharField(max_length=100)
add_coplanner = models.ManyToManyField(User)
trip_description = models.CharField(max_length=1000, default='null')
slug = models.SlugField(max_length=150, default='null')
def __str__(self):
return self.trip_name
def save(self, *args, **kwargs):
self.slug = slugify(self.trip_name)
super().save(*args, **kwargs)
class Activity(models.Model):
trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
activity_name = models.CharField(max_length=100)
date = models.DateField()
time = models.TimeField()
location = models.CharField(max_length=100)
activity_description = models.CharField(max_length=1000, default='null')
item_type = models.CharField(max_length=100)
item_number = models.CharField(max_length=100)
add_cotraveller = models.ManyToManyField(User)
slug = models.SlugField(max_length=150, default='null')
def __str__(self):
return self.activity_name
def save(self):
super(Activity, self).save()
self.slug = '%i-%s' % (
self.id, slugify(self.trip.trip_name)
)
super(Activity, self).save()
Here are my codes in views.py
inside Trips folder:
from django.views import generic
from .models import Trip, Activity
class TripListView(generic.ListView):
template_name = 'trips/triplist.html'
context_object_name = 'all_trips'
def get_queryset(self):
return Trip.objects.all()
class ActivityView(generic.DetailView):
model = Trip
template_name = 'trips/activity.html'
Here are my codes in urls.py
inside Trips folder:
from . import views
from django.urls import path
app_name = 'trips'
urlpatterns = [
path('triplist/', views.TripListView.as_view(), name='triplist'),
path('triplist/<slug:slug>/', views.ActivityView.as_view(), name='activity'),
]
Here are my codes in apps.py
inside trips folder:
from django.apps import AppConfig
class TripsConfig(AppConfig):
name = 'trips'
Here are my codes in apps.py
inside addactivity folder:
from django.apps import AppConfig
class AddactivityConfig(AppConfig):
name = 'addactivity'
Here are my codes in forms.py
inside addactivity folder:
from django.forms import ModelForm
from trips.models import Activity
class ActivityForm(ModelForm):
class Meta:
model = Activity
fields = ['trip', 'activity_name', 'date', 'time', 'location', 'activity_description', 'item_type', 'item_number', 'add_cotraveller']
Here are my codes in forms.py
inside addactivity folder:
from django.forms import ModelForm
from trips.models import Activity
class ActivityForm(ModelForm):
class Meta:
model = Activity
fields = ['trip', 'activity_name', 'date', 'time', 'location', 'activity_description', 'item_type', 'item_number', 'add_cotraveller']
Here are my codes in urls.py
inside addactivity folder:
from . import views
from django.urls import path
app_name = 'addactivity'
urlpatterns = [
path('addactivity/', views.AddActivityFormView.as_view(), name='addactivity'),
]
Here are my codes in views.py
inside addactivity folder:
from django.shortcuts import render
from django.views.generic import TemplateView
from .forms import ActivityForm
from trips.models import Activity
from django.http import HttpResponseRedirect
from django.urls import reverse
# Create your views here.
class AddActivityFormView(TemplateView):
template_name = 'addactivity/addactivityform.html'
def get(self, request):
form = ActivityForm()
activities = Activity.objects.all()
args = {'form': form, 'activities': activities}
return render(request, self.template_name, args)
def post(self, request):
form = ActivityForm(request.POST)
if form.is_valid():
form.save()
trip = form.cleaned_data['trip']
activity_name = form.cleaned_data['activity_name']
date = form.cleaned_data['date']
time = form.cleaned_data['time']
location = form.cleaned_data['location']
activity_description = form.cleaned_data['activity_description']
item_type = form.cleaned_data['item_type']
item_number = form.cleaned_data['item_number']
add_cotraveller = form.cleaned_data['add_cotraveller']
args = {'form': form, 'trip': trip, 'activity_name': activity_name, 'date': date, 'time': time, 'location': location, 'activity_description': activity_description, 'item_type': item_type, 'item_number': item_number, 'add_cotraveller': add_cotraveller}
return HttpResponseRedirect(reverse('trips:activity'))
Here are my codes in addactivityform.html
inside templates > addactivity folder:
{% extends 'base.html' %}
{% load static %}
{% block title %} Add Activity {% endblock %}
{% block content %}
<div class="container">
<h2>Add New Activity</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
</div>
{% endblock %}
Is there any problem in return HttpResponseRedirect(reverse('trips:activity'))
?
In urls.py
inside Trips folder, there is a slug.
path('triplist/<slug:slug>/', views.ActivityView.as_view(), name='activity'),
I can't figure out a way to redirect to a web page with slug with my codes in views.py
inside addactivity folder. Being a beginner, I am unable to solve the issue. So, I need help.
Update: I have added my codes in addactivityform.html
inside templates > addactivity folder in the question.
I also found similar questions and answers at Stackoverflow. But still, I can't solve the issue.
Here is the link to my project files.