0

I dont know how to pass one part of my url address(date) to variable in view. The url address looks almost like: xxxx/user/date e.g. www.dat/kowalski/20160101 and I would like to pass the date 20160101 to variable in views. How to do it? I am new in python and Django.

I created url pattern in urls.py file:

urlpatterns = patterns('',
    .....
    url(r'^tmp/(?P<username>[a-z0-9]+)/(\d{8})/$', views.DateStats.as_view(), name='datestats'),) 

and very basic views but what should I do to save date to variable??

2 Answers 2

3

You do the same as you do with the username: give the capturing group a name.

r'^tmp/(?P<username>[a-z0-9]+)/(?P<date>\d{8})/$'
Sign up to request clarification or add additional context in comments.

4 Comments

I don't understand. Give me part of code to save the date into variable in views.py
This is part of code in urls.py file but I have to use and pass date in views.py file. I got there simply function. How to use <date> in views.py ?
I can't really see what you're not understanding here. Do you have a view? What parameters does it take now? Have you done the tutorial, where all this is fully explained?
Yes, I have the view. It takes def get(self, request, username):
0

If you want to get url parameters in the views:

from django.http import HttpResponse
from django.views.generic import View

class DateStats(View):
    def get(self, request, *args, **kwargs):
        date = self.kwargs.get('date')
        # do something with date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.