Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Abhiram Challapalli
Abhiram Challapalli
5,605 Points

getting an error while creating step_details

when i try to save a step in the course, i am getting an error - Django Basics > Step Details

you can download the screenshots using the below Urls

step_details_page link

error_page_link

please let me know if you need additional information..........

Also, i suspect that the url could be the issue.

urlpatterns = [
    url(r'^$', views.course_list),
    url(r'(?P<pk>\d+)/(?P<pk>\d+)/$', views.step_details),  #SUSPECTING THAT THIS URL COULD BE THE ISSUE
    url(r'(?P<pk>\d+)/$', views.course_details),
]
TypeError at /admin/courses/course/3/change/

__str__ returned non-string (type NoneType)

Request Method:     POST
Request URL:    http://127.0.0.1:8000/admin/courses/course/3/change/
Django Version:     1.11.5
Exception Type:     TypeError
Exception Value:    

__str__ returned non-string (type NoneType)

Exception Location:     C:\Users\AsusGo\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\encoding.py in force_text, line 76
Python Executable:  C:\Users\AsusGo\AppData\Local\Programs\Python\Python36\python.exe
Python Version:     3.6.2
Python Path:    

['C:\\Users\\AsusGo\\Documents\\treehouse_django',
 'C:\\Users\\AsusGo\\Documents\\treehouse_django',
 'C:\\Users\\AsusGo\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
 'C:\\Users\\AsusGo\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
 'C:\\Users\\AsusGo\\AppData\\Local\\Programs\\Python\\Python36\\lib',
 'C:\\Users\\AsusGo\\AppData\\Local\\Programs\\Python\\Python36',
 'C:\\Users\\AsusGo\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']

Url patterns

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.course_list),
    url(r'(?P<pk>\d+)/(?P<pk>\d+)/$', views.step_details),
    url(r'(?P<pk>\d+)/$', views.course_details),
]

models.py

    class Step(models.Model):
    title = models.CharField(max_length = 255)
    description = models.TextField()
    order = models.IntegerField(default=0)
    course = models.ForeignKey(Course)

    class Meta:
        ordering = ['order', ]


    def __str__(self):
        self.title

Views.py

def step_details(request, course_pk, step_pk):
    step = get_object_or_404(Step, course_id = course_pk, pk=step_pk)
    return render(request, 'courses/step_details.html', {'step':step})

step_details.html

{% extends "layout.html"%}


{% block title%} {{step.title}} - {{ step.course.title }} {% endblock title %}

{% block content %}
  <article>
    <h2> {{ step.course.title }} </h2>
    {{ step.description }}
    {{ step.content|linebreaks }}
    {{ step.course.created_at }}

{##}
{#    <section>#}
{#      {% for step in course.step_set.all %}#}
{#        <h3> {{ step.title}}</h3>#}
{#      {{step.description}}#}
{#      {% endfor %}#}
{#    </section>#}
  </article>
{% endblock %}

admin.py

from django.contrib import admin

# Register your models here.

from .models import Course, Step

class StepInline(admin.TabularInline):
    model = Step

class CourseAdmin(admin.ModelAdmin):
    inlines = [StepInline,]

admin.site.register(Course, CourseAdmin)
admin.site.register(Step)

Regards, Abhiram.

1 Answer

Tom Slutsky
Tom Slutsky
12,453 Points

might be indentation in models.py ?

Abhiram Challapalli
Abhiram Challapalli
5,605 Points

thanks for your response...

I am sorry but it is not the indentation.. I must have removed it while pasting the code.

class Course(models.Model):
       created_at = models.DateTimeField(auto_now_add=True)
       title = models.CharField(max_length=255)
       description = models.TextField()

       def __str__(self):
             return self.title

class Step(models.Model):
      title = models.CharField(max_length = 255)
      description = models.TextField()
      order = models.IntegerField(default=0)
      course = models.ForeignKey(Course)

    class Meta:
          ordering = ['order', ]

The course part is working... i am getting an error while saving the step.

could the issue be with the url

urlpatterns = [
    url(r'^$', views.course_list),
    url(r'(?P<pk>\d+)/(?P<pk>\d+)/$', views.step_details),  #SUSPECTING THAT THIS URL COULD BE THE ISSUE
    url(r'(?P<pk>\d+)/$', views.course_details),
]