Reputation: 1186
maybe this is to easy but i'm having troubles.
I need to pass a value in a template.html to a view.py i've already search for this question in google and django docs however the only solution founded was to use: URL (GET) is there another form?
I have this in course_main.html:
{% for Course in Course %}
<div id='courseContainer'>
<h4 class="center"> {{ Course.name }} </h4>
<a href="course/{{ Course.name }}"><img class="center" src="{{ Course.image.url }}"/></a>
<p class="center"> {{ Course.date }} </p>
<p> {{ Course.description }} </p>
<!--End courseContainer -->
</div>
{% endfor %}
So when a user press: <'img class="center" src="{{ Course.image.url }}"/> this redirects to the variable in {{ Course.name }}
and this is handle by explicit_course in the urls.py:
urlpatterns = patterns('',
#Courses
(r'^course/[a-z].*$',explicit_course),
)
Here is explicit_course views.py:
def explicit_course(request):
profesor = Professor.objects.get(id=1)
courseExplicit = Course.objects.get(name="django-python")
variables = RequestContext(request,{
'CourseExplicit':courseExplicit,
'Profesor':profesor
})
return render_to_response('course_explicit.html',variables)
Here i want to do something like this:
courseExplicit = Course.objects.get(name="Course.name")
But i don't know how to pass the Course value from course_main.html to explicit_course in views.py
Can anyone help me?
Thanks a lot.
Upvotes: 1
Views: 2850
Reputation: 6767
You need to change your urls.py
to use a named regex expression:
urlpatterns = patterns('',
#Courses
(r'^course/(?P<course_name>[a-z]+)$',explicit_course),
)
And then change your explicit_course
view to say this:
def explicit_course(request, course_name):
profesor = Professor.objects.get(id=1)
courseExplicit = Course.objects.get(name=course_name)
# etc...
Named regex matches in your urls.py
will pass their contents as variables to the view (after request
), which you can then use as normal.
You're not really 'passing value from template to view', you're just pulling data out of the URL.
Documentation can be found here, it's worth a read: https://docs.djangoproject.com/en/1.5/topics/http/urls/#named-groups
Upvotes: 1