473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

django's view.py as class not just methods

Hi,

I'm relatively new to django and maybe my question is stupid, but...

Is it possible to map in urls.py some url not to function in views.py
(which has first argument with HttpRequest) but to some class method?
In that case each instance of such class would be created when session
starts and for subsequent calls would be served as self ?

I know, I know that HttpRequest has session member and I can use it.
But maybe it would be good idea to have such url ==class.method
mapping.

thanks,
skink
Aug 25 '06 #1
2 2181
Skink <sp**@me.pleasewrites:
Hi,

I'm relatively new to django and maybe my question is stupid, but...

Is it possible to map in urls.py some url not to function in views.py
(which has first argument with HttpRequest) but to some class method?
In that case each instance of such class would be created when session
starts and for subsequent calls would be served as self ?

I know, I know that HttpRequest has session member and I can use it.
But maybe it would be good idea to have such url ==class.method
mapping.
I didn't try it with django but maybe closure is the solution.
Try this:

<code>
#!/usr/bin/env python

class MyView(object):
def __init__(self):
self.visited = []
def index(self, *args):
self.visited.append("index")
print "%s.index: %r" % (self.__class__.__name__, args)
return "response from index"
def detail(self, *args):
self.visited.append("detail")
print "%s.detail: %r" % (self.__class__.__name__, args)
return "response from detail"
def error(self, *args):
self.visited.append("error")
print "%s.error: %r" % (self.__class__.__name__, args)
return "response from error"

def make_view(obj, methodname):
def view(*args):
try:
return getattr(obj, methodname)(*args)
except AttributeError:
return obj.error(*args)
return view

view_obj = MyView()

index = make_view(view_obj, "index")
detail = make_view(view_obj, "detail")
download = make_view(view_obj, "download")

print index("request to index")
print detail("request to detail", 25)
print download("request to download", "abc", 99)
print
print "\n".join(view_obj.visited)
</code>

index, detail and download functions can be mapped
in urls.py now.

--
HTH,
Rob
Aug 25 '06 #2
Rob Wolfe wrote:
>
I didn't try it with django but maybe closure is the solution.
Try this:
index, detail and download functions can be mapped
in urls.py now.
Rob,
thanks a lot. is gonna be good starting point

skink
Aug 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Droolboy | last post by:
I'm trying to build a fairly small (max. 10 different pages) site using php, and it's becoming obvious that I need some kind of model view separation. Having done a few searches, I've come...
28
by: flamesrock | last post by:
Firstly, this topic is NOT intended for trolling or starting any flame wars. I want to know if anyone has experience with these frameworks, and if so, how do they compare? Which one do you...
0
by: George Sakkis | last post by:
Didn't have much luck in the Django list, so I'm posting it here just in case anyone has come up with this problem. Django maintains two parallel hierarchies of field classes, one related to...
13
by: Ilias Lazaridis | last post by:
I have implemented a simple schema evolution support for django, due to a need for a personal project. Additionally, I've provided an Audit: http://case.lazaridis.com/wiki/DjangoAudit As a...
92
by: Ray | last post by:
I just moved to another company that's mainly a Java/.NET shop. I was happy to find out that there's a movement from the grassroot to try to convince the boss to use a dynamic language for our...
2
by: cuongvt | last post by:
Hello I'm new to both Django and Python. I'm mainly developing on PHP. I tend to move to Django. But I want to confirm as below: I heard that Django is mainly used for something like content...
2
by: kang jia | last post by:
hi currently, i am using Django to create webpage, in view.py the code is in the following: def contact(request): if request.method == 'POST': form =...
4
by: K | last post by:
Hello everyone, I understand that urllib and urllib2 serve as really simple page request libraries. I was wondering if there is a library out there that can get the HTTP requests for a given...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.