View Function Signature?

72 views
Skip to first unread message

nickeforos

unread,
May 12, 2017, 9:19:12 AM5/12/17
to Django users
I'm following the django tutorials and documentaion as well as the django book.
In 
there is this view function:
def current_datetime(request):
    now
= datetime.datetime.now()
    html
= "<html><body>It is now %s.</body></html>" % now
   
return HttpResponse(html)

and in:
there is this view function:
def hours_ahead(request, offset):
   
try:
        offset
= int(offset)
   
except ValueError:
       
raise Http404()
    dt
= datetime.datetime.now() + datetime.timedelta(hours=offset)
    html
= "In %s hour(s), it will be  %s." % (offset, dt)
   
return HttpResponse(html)

So in the view function I can pass request as well as an argument from the url but how does the signature of a view function look like?
Where can I find it in the documentation?
Is it possible to see such signatures in PyCharm?

Constantine Covtushenko

unread,
May 12, 2017, 9:43:18 AM5/12/17
to django...@googlegroups.com
Hi,

Please check that link of documentation.
For me it was very helpful to understand all pieces of Request/Response cycle.

Regards,
Constantine C.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/76227b4e-5de3-4ca1-a9c6-658c2ee69529%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Sincerely yours,
Constantine C

Nick Gilmour

unread,
May 12, 2017, 10:11:55 AM5/12/17
to django...@googlegroups.com
Thanks for the link. I understand these examples but still I would like to see how a view function is defined.

I also would like to see it in PyCharm. When I click on a view function in PyCharm I only see:

def myview_function(request)
Inferred type: (request: Any) -> HttpResponse

I would expect to see something like this:
view_function(request, *args, **kwargs)

I've also looked in django's source but I cannot locate it.

Constantine Covtushenko

unread,
May 12, 2017, 3:34:44 PM5/12/17
to django...@googlegroups.com
Hi Nick,

As for me it is responsibility of the developer to define view function.
There is only one mandatory argument 'request'.
All other are based on the logic of view function and url patterns.

Say for view function that operates with entity might be natural to add 'id' as a second parameter.

At the same time for view function that shows blog might be natural to get 'year' as a second parameter.

And so on.

I hope that helps.

Regards,
Constantine C.


For more options, visit https://groups.google.com/d/optout.

Daniel Roseman

unread,
May 13, 2017, 5:15:58 PM5/13/17
to Django users
On Friday, 12 May 2017 15:11:55 UTC+1, nickeforos wrote:
Thanks for the link. I understand these examples but still I would like to see how a view function is defined.

I also would like to see it in PyCharm. When I click on a view function in PyCharm I only see:

def myview_function(request)
Inferred type: (request: Any) -> HttpResponse

I would expect to see something like this:
view_function(request, *args, **kwargs)

I've also looked in django's source but I cannot locate it.


But this isn't how Python works. You don't need a defined signature or prototype for a class of functions. A view is simply called with the request and any parameters passed from the URL, that's all there is to it.
--
DR.

Nick Gilmour

unread,
May 14, 2017, 4:38:01 PM5/14/17
to django...@googlegroups.com
Definition of url:
def url(regex, view, kwargs=None, name=None):

I also found this:
def view(request, *args, **kwargs):

here:
.../django/views/generic/base.py


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

James Bennett

unread,
May 14, 2017, 4:51:45 PM5/14/17
to django...@googlegroups.com
On Sun, May 14, 2017 at 1:36 PM, Nick Gilmour <nicke...@gmail.com> wrote:
Definition of url:
def url(regex, view, kwargs=None, name=None):

I also found this:
def view(request, *args, **kwargs):


Neither one of these is what was being asked for.

And like several people have said, what's being asked for doesn't exist: a Django view is defined as a callable Python object which takes as its first position argument an HttpRequest object, may or may not accept additional positional and/or keyword arguments, and either returns an HttpResponse object or raises an exception. That definition does not allow for a single universal argument signature that will work on all views.

Nick Gilmour

unread,
May 14, 2017, 5:51:58 PM5/14/17
to django...@googlegroups.com
Neither one of these is what was being asked for.

I didn't say I have found what I was looking for.

All answers are sufficient to me for now. Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages