Hi,

I do something similar to Julien solution.
Have a "views" directory and put individual files inside this directory.
By individual files I mean, i tend to split views by operations
that apply to a flow for webpages using the same models.
I also put specific ajax view methods inside their own view files.

For URL files. A also started to split these files inside applications,
instead of having
a fat one with all url combinations for the same application/module
 i split them for instance in ajax an non-ajax ones.

I also had
the problem of maintaining 2 very identical view methods doing the same but
refactored these ones into a common method inside a
common_view_methods.py file.
These methods do the bulk work and are called by the view methods.
Then the view return the right content_type to the browser, that way i
have extra imports
but avoid the extra 'if' statement and keep code cleaner.

Im also spliting forms and models. If  i have a complex form (a form that
maps to
more than one model)
i prefer to just have one per file. newforms are great but if you want
a complex form layout
(specific form validations,  html attributes like size, maxlength, styles
and classes applied differently to label and
inputs), you got to get your hands really dirty and type some
code. the default
stock options just do or generate simple and dull html code.

Django is very flexible regarding the way you organize your files and
code, there's
nothing preventing us from using just one file (models, views, urls,
forms) for the all
application/module code. The organization layout presented in the
documentation are
just a simple guideline of how to keeping things simple. But as soon as you add
models and functionalities each file start to grow and i hate to have
to scroll a lot and
search for a specific line /method inside a large file.

In the end we have to IMPORT everything we need to use so we have the
freedom to decide where to put
them, theres no magic paths like other frameworks. And i like this!

Just my thoughts

Jorge

On 1/30/08, Julien <[EMAIL PROTECTED]> wrote:
>
>
> Hey there,
>
> My approach is not so much of an effort, and I do not write two
> versions of every method. My ajax views are purely and only destined
> for Ajax stuff. In this case, for example, you could imagine having a
> DIV refreshing its content every 30 seconds by calling the
> ajax_latest_news view.
>
> Your approach is interesting, on giving the client the choice of
> format for the result (json, XMl or whatever). However, instead of
> adding 'wants' field in POST, maybe should you use different URLs
> (e.g. latest_news/json/, latest_news/xml/, etc.) which would all point
> to the same view but with a different parameter. I'd find it cleaner
> than way.
>
> Cheers,
>
> Julien
>
> On Jan 30, 10:20 am, "Vance Dubberly" <[EMAIL PROTECTED]> wrote:
> > Interesting and organized but it seems like a lot of work. Do you
> > write two versions of every method?
> >
> > For instance
> >
> > def ajax_latest_news(request):
> >    ...
> >    latest_news = ...
> >    json = simplejson.dumps(latest_news)
> >    return HttpResponse(json, mimetype='application/json')
> >
> > and also
> >
> > def latest_news(request):
> >    ...
> >    latest_news = ...
> >    return render_to_response......
> >
> > My personal way to deal with this has to be make all ajax requests
> > POST's and add a wants field to them, then to do something like this:
> >
> > def latest_news(request):
> >
> >   latest_news = ....
> >
> >   if request.method == 'POST' and request.POST.has_key('wants'):
> >     if request.POST.has_key('wants') == 'json'
> >       json = simplejson.dumps(latest_news)
> >       return HttpResponse(json, mimetype='application/json')
> >     else:
> >       normal template stuff....
> >
> > It's kinda gludgy but I've not had time work it out yet but it seems
> > the prime way to do this would be to add a custom header to the
> > request that read 'accept-content-type='text/javascript'  and then
> > work off that but....
> >
> > I'm of course not happy with my solution so am interested in seeing
> > what other people feed back...
> >
> > Vance
> >
> >   --
> >
> > On Jan 29, 2008 2:37 PM, Julien <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> >
> >
> > > Hi all,
> >
> > > I've started using Ajax in my Django sites. I'm using Jquery (and I
> > > love it!), but here it doesn't really matter which JS library you use,
> > > I'd just like to know your opinions on the best way to go to handle it
> > > on the server side.
> >
> > > I'm gonna tell you how I do it, then I'd really appreciate if you
> > > could share the way you do it. There are probably many good
> > > approaches, but maybe we could find some common ground that could for
> > > example be put online on the wiki.
> >
> > > What I do is mostly cosmetic to keep things tidy:
> >
> > > 1) I put all my ajax views in a separate file 'ajax-views.py'. By
> > > "ajax views" I mean all views that are called by a javascript, as
> > > opposed to the traditional views which are called through the
> > > browser's address bar.
> > > The structure then looks like this:
> > > myapp
> > >    |_ ajax-views.py
> > >    |_ models.py
> > >    |_ urls.py
> > >    |_ views.py
> >
> > > 2) In the URLConf I also separate traditional views from ajax views:
> >
> > > from django.conf.urls.defaults import *
> >
> > > # Traditional views
> > > urlpatterns = patterns('myapp.views',
> > >    url(r'^$', 'home'),
> > >    url(r'^news/$', 'list_news'),
> > > )
> >
> > > # AJAX views
> > > urlpatterns += patterns('myapp.ajax-views',         # Don't forget the
> > > '+=' sign here.
> > >    url(r'^ajax/news/latest/$', 'ajax_latest_news'),
> > >    url(r'^ajax/news/add/$', 'ajax_add_news'),
> > > )
> >
> > > Note that I also add the prefix "ajax/" in the URLs for the ajax
> > > views.
> >
> > > 3) The ajax views can look like this:
> >
> > > from django.utils import simplejson
> >
> > > def ajax_latest_news(request):
> > >     ...
> > >     latest_news = ...
> > >     json = simplejson.dumps(latest_news)
> > >     return HttpResponse(json, mimetype='application/json')
> >
> > > def ajax_add_news(request):
> > >     ...
> > >     results = {'success':True}
> > >     json = simplejson.dumps(results)
> > >     return HttpResponse(json, mimetype='application/json')
> >
> > > Please let me know your thoughts ;)
> > > Cheers!
> >
> > > Julien
> >
> > --
> > To pretend, I actually do the thing: I have therefore only pretended to
> pretend.
> >   - Jacques Derrida
> >
>


-- 
-------------------------------
Jorge Sousa

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to