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 trialElias Helander
1,461 PointsI have 3 classes related to each other. A - B - C. B is related to pk of A and C is related to pk of B. URL structure?
I am trying to create a function in django called Guide.
There is 3 classes. Guide Step Subtask
Each guide is mad of X steps. Each step is made of X subtasks.
I get error when I try to create a new Step and the same error when I create the subtask.
Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'taskoftheday/(?P<guide_id>[0-9]+)/$']
Here is part of my HTML:
<ul class="nav nav-pills" style="margin-bottom: 10px;">
<li role="presentation" class="active"><a href="{% url 'taskoftheday:detail' step.id %}">View All</a></li>
<li role="presentation"><a href="{% url 'taskoftheday:create_subtask' step.id %}">Add New Subtask</a></li>
</ul>
--------------------
<ul class="nav nav-pills" style="margin-bottom: 10px;">
<li role="presentation" class="active"><a href="{% url 'taskoftheday:detail' guide.id %}">View All</a></li>
<li role="presentation"><a href="{% url 'taskoftheday:create_step' guide.id %}">Add New Step</a></li>
</ul>
Python code in urls.py
url(r'^(?P<guide_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<step_id>[0-9]+)/complete/$', views.complete, name='complete'),
url(r'^steps/(?P<filter_by>[a-zA_Z]+)/$', views.steps, name='steps'),
url(r'^steps/(?P<filter_by>[a-zA_Z]+)/$', views.subtasks, name='subtasks'),
url(r'^create_guide/$', views.create_guide, name='create_guide'),
url(r'^(?P<guide_id>[0-9]+)/create_step/$', views.create_step, name='create_step'),
url(r'^(?P<step_id>[0-9]+)/create_subtask/$', views.create_subtask, name='create_subtask'),
url(r'^(?P<guide_id>[0-9]+)/delete_step/(?P<step_id>[0-9]+)/$', views.delete_step, name='delete_step'),
url(r'^(?P<step_id>[0-9]+)/delete_subtask/(?P<subtask_id>[0-9]+)/$', views.delete_subtask, name='delete_subtask'),
url(r'^(?P<guide_id>[0-9]+)/complete_guide/$', views.complete_guide, name='complete_guide'),
url(r'^(?P<guide_id>[0-9]+)/delete_guide/$', views.delete_guide, name='delete_guide'),
Python code in views.py
def steps(request, filter_by):
if not request.user.is_authenticated():
return render(request, 'taskoftheday/login.html')
else:
try:
step_ids = []
for guide in Guide.objects.filter(user=request.user):
for step in guide.step_set.all():
step_ids.append(step.pk)
users_steps = Step.objects.filter(pk__in=step_ids)
if filter_by == 'completes':
users_steps = users_steps.filter(is_complete=True)
except Guide.DoesNotExist:
users_steps = []
return render(request, 'taskoftheday/steps.html', {
'step_list': users_steps,
'filter_by': filter_by,
})
def subtasks(request, filter_by):
if not request.user.is_authenticated():
return render(request, 'taskoftheday/login.html')
else:
try:
subtask_ids = []
for step in Step.objects.filter(user=request.user):
for subtask in step.subtask_set.all():
subtask_ids.append(subtask.pk)
users_subtasks = Subtask.objects.filter(pk__in=subtask_ids)
if filter_by == 'completes':
users_subtasks = users_subtasks.filter(is_complete=True)
except Guide.DoesNotExist:
users_steps = []
return render(request, 'taskoftheday/steps.html', {
'step_list': users_subtasks,
'filter_by': filter_by,
})
def create_step(request, guide_id):
form = StepForm(request.POST)
guide = get_object_or_404(Guide, pk=guide_id)
if form.is_valid():
guides_steps = guide.step_set.all()
for s in guides_steps:
if s.step_title == form.cleaned_data.get("step_title"):
context = {
'guide': guide,
'form': form,
'error_message': 'You already added that step',
}
return render(request, 'taskoftheday/create_step.html', context)
step = form.save(commit=False)
step.guide = guide
step.save()
return render(request, 'taskoftheday/detail.html', {'guide': guide})
context = {
'guide': guide,
'form': form,
}
return render(request, 'taskoftheday/create_step.html', context)
def create_subtask(request, step_id):
form = StepForm(request.POST)
step = get_object_or_404(Step, pk=step_id)
if form.is_valid():
steps_subtasks = step.subtask_set.all()
for s in steps_subtasks:
if s.subtask_title == form.cleaned_data.get("subtask_title"):
context = {
'step': step,
'form': form,
'error_message': 'You already added that subtask',
}
return render(request, 'taskoftheday/create_subtask.html', context)
subtask = form.save(commit=False)
subtask.step = step
subtask.save()
return render(request, 'taskoftheday/detail.html', {'step': step})
context = {
'step': step,
'form': form,
}
return render(request, 'taskoftheday/create_subtask.html', context)
Thanks for any answers :D
[MOD: added ```python formatting -cf]
Elias Helander
1,461 PointsElias Helander
1,461 PointsHere is a Zip of the whole Django project.
https://drive.google.com/file/d/0B7gn5JUjcOFNS3NmQU8wcE1CRzQ/view?usp=sharing