Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Generalizing functions in views.py? [RSS Feed]

#1 Jan. 7, 2011 17:19:17

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generalizing functions in views.py?


Greetings!

I've been spending the past few weeks learning django and love it! I'm
running into a snag though...

I'm using just one template for all my toying right now, and it has a few
sections in it that looks something like this:

{% if user.username %}
<center><h3>Welcome {{ user.username }}!</h3></center>
{% else %}
<center>
<h3>Welcome anonymous user!</h3>
<h2>Please /login/ login </h2>
</center>
{% endif %}

-- and --

{% for link in link_list %}
{{ link.href }} {{ link.name }}
{% if not forloop.last %}
|
{% endif %}
{% endfor %}


I have around a dozen views, and I'm noticing that in each of my views I'm
having to do something like:

return render_to_response('base.html', { #various logic
'link_list': link_list,
'user': request.user })

I have link_list defined in another file, so each function can handle that
properly. The problem creeps up on the 'user': request.user logic though.
Instead of having to have this line of code in EVERY return statement, I'd
rather just define it once in a dictionary at the top of my views.py page or
in another file, with something like this:

dictionary_of_variables = {'user': request.user, 'link_list': link_list,
#various other global things}

I'd also like to just return something like this in each of my functions:

business_logic_variable = "some business logic"
dictionary_of_variables = business_logic_variable
return render_to_response('base.html', dictionary_of_variables)


HOWEVER: I can't do that because request.user is out of the scope of a
'request' that you would typically pass to a view function. My specific
error is:

name 'request' is not defined


Is there a workaround for this sort of logic? I don't mind returning it in
my 10 or so functions that I have right now, but when my webpage grows to
hundreds of functions & pages, it would be much better to globally define
this variable.

Thanks for your reply!

--
View this message in context:http://old.nabble.com/Generalizing-functions-in-views.py--tp30616200p30616200.htmlSent from the django-users mailing list archive at Nabble.com.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 7, 2011 18:25:43

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generalizing functions in views.py?


On Friday, January 7, 2011 5:19:10 PM UTC, Joe0418 wrote:
>
>
> Greetings!
>
> I've been spending the past few weeks learning django and love it! I'm
> running into a snag though...
>
> I'm using just one template for all my toying right now, and it has a few
> sections in it that looks something like this:
>
> {% if user.username %}
> <center><h3>Welcome {{ user.username }}!</h3></center>
> {% else %}
> <center>
> <h3>Welcome anonymous user!</h3>
> <h2>Please /login/ login </h2>
> </center>
> {% endif %}
>
> -- and --
>
> {% for link in link_list %}
> {{ link.href }} {{ link.name }}
> {% if not forloop.last %}
> |
> {% endif %}
> {% endfor %}
>
>
> I have around a dozen views, and I'm noticing that in each of my views I'm
> having to do something like:
>
> return render_to_response('base.html', { #various logic
> 'link_list': link_list,
> 'user': request.user })
>
> I have link_list defined in another file, so each function can handle that
> properly. The problem creeps up on the 'user': request.user logic though.
> Instead of having to have this line of code in EVERY return statement, I'd
> rather just define it once in a dictionary at the top of my views.py page
> or
> in another file, with something like this:
>
> dictionary_of_variables = {'user': request.user, 'link_list': link_list,
> #various other global things}
>
> I'd also like to just return something like this in each of my functions:
>
> business_logic_variable = "some business logic"
> dictionary_of_variables = business_logic_variable
> return render_to_response('base.html', dictionary_of_variables)
>
>
> HOWEVER: I can't do that because request.user is out of the scope of a
> 'request' that you would typically pass to a view function. My specific
> error is:
>
> name 'request' is not defined
>
>
> Is there a workaround for this sort of logic? I don't mind returning it in
> my 10 or so functions that I have right now, but when my webpage grows to
> hundreds of functions & pages, it would be much better to globally define
> this variable.
>
> Thanks for your reply!
>

Context processors are what you need.http://docs.djangoproject.com/en/1.2/ref/templates/api/#subclassing-context-requestcontext--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 18th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support