Azoraqua
Azoraqua

Reputation: 49

Forward url arguments to view in Django

I am trying to forward projects/1 to the view, for purpose of getting the project based on the id. I have tried to change the regex, and the name.

Edit: But the issue is: The id doesn't get forwarded (Or atleast not correctly) to the views, which results in the filter failing to find the Project based on the id.

Urls related to the 'Project':

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^project/(?P<project_id>([0-9]+))$', views.project, name='project'),
]  

The view for the 'Project':

def project(request, project_id=1):
    template = loader.get_template("project.html")
    context = {
        'project': Project.objects.filter(project_id=project_id)
    }

    return HttpResponse(template.render(context, request))

Upvotes: 0

Views: 37

Answers (1)

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13733

The regex seem a little incorrect to me, especially parentheses. Try this:

url(r'^project/(?P<project_id>[0-9]+)/$', views.project, name='project'),

UPDATE:

So you want to show only one project instead of several. In that case, change Project.objects.filter to Project.objects.get in your view.

Also, try this awesome django tutorial to learn the basics

Hope it helps.

Upvotes: 2

Related Questions