Can I use Components and Classes in Django for views/controllers?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I use Components and Classes in Django for views/controllers?

Ståle Undheim-5

I come from a Java background, and I really like Apache Wicket as a
way for constructing web frameworks. In it you use classes, extension
and compositions to build your pages. This allows for great reuse off
components in different pages.

Django however works via functions instead, which can't be extended.
And since each function will represent a full page, they seem harder
to combine together. Maybe the way I am thinking about how to solve
stuff in Django is too much affected by my Java background, and I am
not doing things in a Pythonic way.

So I have 2 cases, I will explain the problem, how I would solve it in
Wicket, how I have solved it in Python and the issues I have with the
solutions. Maybe someone can give me some good pointers and advice
regarding this.

The first thing I have is a file list. I use this in 2 different
places. The list has some filter checkboxes on top, and a table off
files, each with a link next to them. In 1 view the link says "view"
and takes you to viewing the file, in another view it adds the file to
a selection off files. In Wicket I would make a file list component,
this had 2 properties you needed to set, link text and onclick
action.

For Django I instead ended up just copying the template and fitting it
for each case. Each view has a different base template they extend
which is the context for the application. I also needed to make sure
the context was correct in each view function. I used some javascript,
and was able to copy that directly. I really don't like that I have to
copy the template code around, and match up the view context to this
template in each case. Is it possible to turn my file list into a
component with just 1 template and some config, without it being
excessively verbose?

The other thing I have is multiple views in one app that use the same
base design template. The base design template provides menus related
to the app, while each view provides the content in the design. Since
there is common context variables that are needed for the design
template for each view, each view must include these variables in it's
context for things to work. Which I again feel like is duplication off
code. I would instead prefered a base class for the app, that all the
views in it extended, and thus didn't need to worry about setting up
the root context correctly.

I hope someone can enlighten me about me either being all wrong in how
I think about app construction, or that there are good elegant
solutions for these things already available.

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

Reply | Threaded
Open this post in threaded view
|

Re: Can I use Components and Classes in Django for views/controllers?

googletorp

Hi.
For the second part, it looks like you could use your own
template_context_preprocessor. With it you can do stuff like
creating variables for your template. Maybe this would be able to
solve your problem having to send the same template vars over and
over again. If you dont need view specific variables, this should do
it.

You can take a look at the docs about them and how to write them:
http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

~Jakob


On 1 Apr., 09:42, Ståle Undheim <[hidden email]> wrote:

> I come from a Java background, and I really like Apache Wicket as a
> way for constructing web frameworks. In it you use classes, extension
> and compositions to build your pages. This allows for great reuse off
> components in different pages.
>
> Django however works via functions instead, which can't be extended.
> And since each function will represent a full page, they seem harder
> to combine together. Maybe the way I am thinking about how to solve
> stuff in Django is too much affected by my Java background, and I am
> not doing things in a Pythonic way.
>
> So I have 2 cases, I will explain the problem, how I would solve it in
> Wicket, how I have solved it in Python and the issues I have with the
> solutions. Maybe someone can give me some good pointers and advice
> regarding this.
>
> The first thing I have is a file list. I use this in 2 different
> places. The list has some filter checkboxes on top, and a table off
> files, each with a link next to them. In 1 view the link says "view"
> and takes you to viewing the file, in another view it adds the file to
> a selection off files. In Wicket I would make a file list component,
> this had 2 properties you needed to set, link text and onclick
> action.
>
> For Django I instead ended up just copying the template and fitting it
> for each case. Each view has a different base template they extend
> which is the context for the application. I also needed to make sure
> the context was correct in each view function. I used some javascript,
> and was able to copy that directly. I really don't like that I have to
> copy the template code around, and match up the view context to this
> template in each case. Is it possible to turn my file list into a
> component with just 1 template and some config, without it being
> excessively verbose?
>
> The other thing I have is multiple views in one app that use the same
> base design template. The base design template provides menus related
> to the app, while each view provides the content in the design. Since
> there is common context variables that are needed for the design
> template for each view, each view must include these variables in it's
> context for things to work. Which I again feel like is duplication off
> code. I would instead prefered a base class for the app, that all the
> views in it extended, and thus didn't need to worry about setting up
> the root context correctly.
>
> I hope someone can enlighten me about me either being all wrong in how
> I think about app construction, or that there are good elegant
> solutions for these things already available.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Can I use Components and Classes in Django for views/controllers?

Ståle Undheim-5

I have thought about this, the problem is that part off the context
variables are arguments to the view method. So I have urls like "(?
P<project>\w+)", and I need to extract the project name from the url
to produce the context. So in my context preprocessor to read the url
configs, look if a pattern matches the current path, and if it does,
extract the project from the url if present in the path. It all seems
a bit hackish to me, even though it's possible.

I can proabably also use decorators to achieve the same, but in truth
I would prefer it if I could extend some class instead.

On Apr 1, 10:51 am, Briel <[hidden email]> wrote:

> Hi.
> For the second part, it looks like you could use your own
> template_context_preprocessor. With it you can do stuff like
> creating variables for your template. Maybe this would be able to
> solve your problem having to send the same template vars over and
> over again. If you dont need view specific variables, this should do
> it.
>
> You can take a look at the docs about them and how to write them:http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-...
>
> ~Jakob
>
> On 1 Apr., 09:42, Ståle Undheim <[hidden email]> wrote:
>
> > I come from a Java background, and I really like Apache Wicket as a
> > way for constructing web frameworks. In it you use classes, extension
> > and compositions to build your pages. This allows for great reuse off
> > components in different pages.
>
> > Django however works via functions instead, which can't be extended.
> > And since each function will represent a full page, they seem harder
> > to combine together. Maybe the way I am thinking about how to solve
> > stuff in Django is too much affected by my Java background, and I am
> > not doing things in a Pythonic way.
>
> > So I have 2 cases, I will explain the problem, how I would solve it in
> > Wicket, how I have solved it in Python and the issues I have with the
> > solutions. Maybe someone can give me some good pointers and advice
> > regarding this.
>
> > The first thing I have is a file list. I use this in 2 different
> > places. The list has some filter checkboxes on top, and a table off
> > files, each with a link next to them. In 1 view the link says "view"
> > and takes you to viewing the file, in another view it adds the file to
> > a selection off files. In Wicket I would make a file list component,
> > this had 2 properties you needed to set, link text and onclick
> > action.
>
> > For Django I instead ended up just copying the template and fitting it
> > for each case. Each view has a different base template they extend
> > which is the context for the application. I also needed to make sure
> > the context was correct in each view function. I used some javascript,
> > and was able to copy that directly. I really don't like that I have to
> > copy the template code around, and match up the view context to this
> > template in each case. Is it possible to turn my file list into a
> > component with just 1 template and some config, without it being
> > excessively verbose?
>
> > The other thing I have is multiple views in one app that use the same
> > base design template. The base design template provides menus related
> > to the app, while each view provides the content in the design. Since
> > there is common context variables that are needed for the design
> > template for each view, each view must include these variables in it's
> > context for things to work. Which I again feel like is duplication off
> > code. I would instead prefered a base class for the app, that all the
> > views in it extended, and thus didn't need to worry about setting up
> > the root context correctly.
>
> > I hope someone can enlighten me about me either being all wrong in how
> > I think about app construction, or that there are good elegant
> > solutions for these things already available.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Can I use Components and Classes in Django for views/controllers?

Andy Baker-2
In reply to this post by Ståle Undheim-5

Without really examining the problem in detail it doesn't sound like
you are doing anything that isn't possible with a combination of
context_processors (for common variables) and template inheritance/
includes.

Each view constructs the specific context, a custom context processor
provides site variables and template inheritance gets you out of copy
and pasting.

Views can also be class or instance methods. As long as they are
callables Django doesn't care what they are.

Also - even function-based views can be stubs that use any amount of
object inheritance manage to construct their context.

Andy

On Apr 1, 8:42 am, Ståle Undheim <[hidden email]> wrote:

> I come from a Java background, and I really like Apache Wicket as a
> way for constructing web frameworks. In it you use classes, extension
> and compositions to build your pages. This allows for great reuse off
> components in different pages.
>
> Django however works via functions instead, which can't be extended.
> And since each function will represent a full page, they seem harder
> to combine together. Maybe the way I am thinking about how to solve
> stuff in Django is too much affected by my Java background, and I am
> not doing things in a Pythonic way.
>
> So I have 2 cases, I will explain the problem, how I would solve it in
> Wicket, how I have solved it in Python and the issues I have with the
> solutions. Maybe someone can give me some good pointers and advice
> regarding this.
>
> The first thing I have is a file list. I use this in 2 different
> places. The list has some filter checkboxes on top, and a table off
> files, each with a link next to them. In 1 view the link says "view"
> and takes you to viewing the file, in another view it adds the file to
> a selection off files. In Wicket I would make a file list component,
> this had 2 properties you needed to set, link text and onclick
> action.
>
> For Django I instead ended up just copying the template and fitting it
> for each case. Each view has a different base template they extend
> which is the context for the application. I also needed to make sure
> the context was correct in each view function. I used some javascript,
> and was able to copy that directly. I really don't like that I have to
> copy the template code around, and match up the view context to this
> template in each case. Is it possible to turn my file list into a
> component with just 1 template and some config, without it being
> excessively verbose?
>
> The other thing I have is multiple views in one app that use the same
> base design template. The base design template provides menus related
> to the app, while each view provides the content in the design. Since
> there is common context variables that are needed for the design
> template for each view, each view must include these variables in it's
> context for things to work. Which I again feel like is duplication off
> code. I would instead prefered a base class for the app, that all the
> views in it extended, and thus didn't need to worry about setting up
> the root context correctly.
>
> I hope someone can enlighten me about me either being all wrong in how
> I think about app construction, or that there are good elegant
> solutions for these things already available.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Can I use Components and Classes in Django for views/controllers?

matehat
In reply to this post by Ståle Undheim-5

Hi,

You do have a couple of options, as mentioned by others earlier. One
of them is creating a base callable class view for your entire site
and have it subclassed for specific cases, but I think that would be a
bit heavy to work with here. Keep in mind that's entirely possible,
but you might find yourself writing more code just to make everything
work (with inheritance and all), unless it really fits your need.
Also, you could subclass the Context class itself, which I think could
be a nice option if you do include the same template variable over and
over and cannot put them in a context_processor.

Check out that example : http://dpaste.com/hold/22238/

As you know, adding more and more reusable variables would only be a
matter of trivial subclassing. Oh, and BTW, if you do need
context_processors, you should subclass RequestContext instead of
Context.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Can I use Components and Classes in Django for views/controllers?

Ariel Nunez
Yes, Class Based Views are nice, here is a fresh blog post about it:




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