Reputation: 2136
In my template I have the following:
{% block content %}
{% for category in categories %}
{% url 'post' category as url %}
<a href="{{ url }}">
<div class="card">
<img src="" alt="Avatar">
<div class="container">
<h4><b>{{ category }}</b></h4>
</div>
</div>
</a>
{% endfor %}
{% endblock %}
The page renders fine when I run the local development server, however when I click on any of the links, nothing happens.
Here is my urls.py file:
urlpatterns = [
url(r'^all/', views.all_jobs, name="all"),
url(r'^post/', views.pick_category, name="post_category"),
url(r'^post/(?P<category>[a-zA-Z]+)/$', views.post_job, name="post"),
url(r'^job/(?P<job_pk>\d+)/$', views.get_job, name="get_job"),
]
Why is this not working?
Thanks in advance.
UPDATE:
I have changed the view code to the following and now the correct url appears but still the view function is not called:
<a href="{% url 'jobs:post' category %}">
My view looks like this:
def post_job(request, category):
form = find_type_of_form()
if request.method == 'POST':
form = find_type_of_form(category, request.POST)
if form.is_valid():
job = form.save()
messages.add_message(request, messages.SUCCESS, "Job successfully posted!")
return HttpResponseRedirect(job.get_absolute_url())
return render(request, 'jobs/post_job_form.html', {'form': form})
def find_type_of_form(category, request_type=None):
if category == categories[0]:
return forms.BabysittingForm(request_type)
elif category == categories[1]:
return forms.TutoringForm(request_type)
elif category == categories[2]:
return forms.PetsittingForm(request_type)
elif category == categories[3]:
return forms.ShoppingForm(request_type)
Upvotes: 0
Views: 284
Reputation: 599600
You haven't terminated your pick_category URL pattern, so it matches everything beginning with "post/". . Make sure you use a trailing dollar sign:
url(r'^post/$', views.pick_category, name="post_category"),
Upvotes: 1
Reputation: 4194
I think you has a problem in this code {% url 'post' category as url %}
, I suggest you to change with this;
{% for category in categories %}
{% url 'post' category=category.slug %} {# an example using slug field #}
<a href="{{ url }}">
<div class="card">
<img src="" alt="Avatar">
<div class="container">
<h4><b>{{ category }}</b></h4>
</div>
</div>
</a>
{% endfor %}
As a note, don't use object as the url.
Upvotes: 0