The Django Djitney

I am currently working on the django project and for some reason my code will not run on the localhost server. I started having problems after the first few steps when I imported things. I kept going thinking I just would not be able to see the progress but step 12 actually has me click a button on the server itself to add information. If it will not load, than I cannot complete it.

I lined up my code with the Youtube solution. Am I missing something?

Link to Exercise: https://www.codecademy.com/paths/build-python-web-apps-with-django/tracks/views-in-django/modules/django-writing-more-views/projects/the-django-djitney

Link to Youtube Solution:

views.py

from django.shortcuts import render
from .models import Line, Station, Stop
from .forms import  StopForm, LineForm, StationForm
# Add your imports below:
from django.views.generic import TemplateView,ListView
from django.views.edit import CreateView, UpdateView, DeleteView

class HomeView(TemplateView):
  template_name = "routes/home.html"

  def get_context_data(self):
    context = super().get_context_data()
    context["lines"] = Line.objects.all()
    context["stations"] = Station.objects.all()
    context["stops"] = Stop.objects.all()
    return context

# Create your views here.
class LinesView(ListView):
  template_name = "routes/lines.html"
  model = Line

# step 8
class CreateLineView(CreateView):
  model = Line
  template_name = "routes/add_line.html"
  form_class = "LineForm"
# Create View creates a new record in the database using a provided Django form. It handles both GET (displaying the form) and POST(processing the submitted data) requests.

# step 9
class UpdateLineView(UpdateView):
  model = Line
  template_name = 'routes/update_line.html'
  form_class = LineForm

# step 10
class DeleteLineView(DeleteView):
  model = Line
  template_name = 'routes/delete_line.html'
  success_url = "/lines"
# There are files in the routes folder for create, update, and delete.

urls.py

from django.urls import path

from . import views

urlpatterns = [
  path('', views.HomeView.as_view(), name='home'),
  # step 6
  path("lines/", views.LinesView.as_view()
  , name = "lines"),
  # step 7 
  path('lines/new/', views.CreateLineView.as_view(), name = 'create_line'),
  # step 8
  path('lines/<pk>/update/', views.UpdateLineView.as_view(), name = 'update_line'),
  # step 10
  path('lines/<pk>/delete', views.DeleteLineView.as_view(), name = 'delete_line'),
]
# I found the name = value in the routes/lines.html. 
# mark question what is the syntax of the path line.
# path(route, view, kwargs=None, name=None)

models.py

from django.db import models


class Line(models.Model):
  name = models.CharField(unique=True, max_length=200)

  def get_absolute_url(self):
    return "/lines"

  def __str__(self):
    return f"{self.name}"


class Station(models.Model):
  name = models.CharField(unique=True, max_length=200)
  accessible = models.BooleanField(default=False)

  def get_absolute_url(self):
    return "/stations"

  def __str__(self):
    return f"{self.name}{' (♿)' if self.accessible else ''}"


class Stop(models.Model):
  # The snippet of code below ensures that no line can have two stops with the same stop number
  class Meta:
    unique_together = (('line', 'stop_number'))

  def get_absolute_url(self):
    return "/stops"

  line = models.ForeignKey(Line, on_delete=models.CASCADE)
  station = models.ForeignKey(Station, on_delete=models.CASCADE)
  stop_number = models.PositiveIntegerField()

  def __str__(self):
    return f"{self.line.name} -- {self.station.name} [{self.stop_number}]"

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Bug detected and fixed