Re: global variable issue

2008-06-24 Thread csmithmaui

Ok...guys...I think I understand what you are saying. It gives me much
relief to understand this better. I didn't realize that the server
would run multiple instances of django to serve my requests. It seems
I had a fundamental misunderstanding of how things work on the server.
I will try to do some reading on how the server fulfills my requests.
Thanks for stopping my insanity of trying to get this to work and
pointing me in the right direction. I think I can make some headway
with this new information.

Gratefully,
csmithmaui
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: global variable issue

2008-06-24 Thread Norman Harman

[EMAIL PROTECTED] wrote:
> If I hit Ctrl-R again it puts my name back, and so on. It's like there
> there are two copies of the global that I am accessing. I can't figure
> this out.  Any ideas? Thanks so much!

Rule 1: Don't use globals.
Rule 2: Don't try to store state in a stateless server, use persistent 
storage like a DB.

There *are* two or more copies of the global.  There are as many copies 
as WebFaction runs instances of django (mod_python or whatever)

It works in dev cause manage.py runserver only starts one instance of 
django.

-- 
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___
You've got fun!  Check out Austin360.com for all the entertainment
info you need to live it up in the big city!

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: global variable issue

2008-06-24 Thread Rajesh Dhawan

Hi,

> So, this all works fine on the development server on my laptop using
> the latest django trunk, but on webfactions server it doesn't work all
> of the time.  Sometimes when I access my object_detail( by the way I
> also wrote my own freeform.html in templates/comments and modified it
> to use the fake_username as the person_name) it shows the name and
> then if I use Ctrl-R in firefox to refresh the page it shows nothing.
> If I hit Ctrl-R again it puts my name back, and so on. It's like there
> there are two copies of the global that I am accessing. I can't figure
> this out.  Any ideas? Thanks so much!

The global variable you've defined is global "per-process". It's
likely that you've two or more processes setup to serve your Django
requests. In that case, there's no guarantee that every request from
one user will always go through the same process. This is what you are
seeing.

In general, it's not a good idea to use globals in this way. You have
a few options:

The simplest one is to store that variable's value in the request
session. It will not be an application level global but you will get
the same value for it for any number of requests made by one user
during his session with your site.

If I misunderstood what you are trying to do and you need a truly
global storage for this value, you could store that value in:

- your DB or
- memcached (assuming it's possible to install and run memcached with
your hosting account) or
- a file on the filesystem

-Rajesh D
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



global variable issue

2008-06-24 Thread csmithmaui



Hi,
  I have an error that is stumping me.  It may be a django or python
issue and nothing to do with webfaction butI have been working on it
for a while now and don't know where else to turn. I am fairly new to
Python and Django so it might be obvious to someone more experienced.
I have a global variable defined in a file called globvars.py:

Code:

alias = {'fake_username':''}

Then I have a view where I try and set the variable based upon
whatever the user enters as a username(I know this is a little weird
but this site is only for friends and family so I just figured they
could use one account and enter any name they want to enter with and I
would change it to generic_user which I have set up in my admin
page).  Looking back it would have been easier to just implement some
easy way of registering everyone, but I already have this mostly
working. Anyway, the global gets set and then I want to save their
username they entered with to use in the blog when they decide to
comment.

Code:

from mysite.globvars import *

def my_login_view(request, template_name='registration/
standard_login.html'):
"Displays the login form and handles the login action."
if request.POST:
request.session.set_test_cookie()
if request.session.test_cookie_worked():
username = 'generic_user'
alias['fake_username'] = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
request.session.delete_test_cookie()
return HttpResponseRedirect('/')
else:
return render_to_response('registration/
auth_failed_login.html', context_instance=RequestContext(request))
else:
return render_to_response('registration/
cookie_failed_login.html', context_instance=RequestContext(request))
return render_to_response(template_name,
context_instance=RequestContext(request))

I am trying to pass the alias global that I captured at login into the
object_detail_wrapper via extra_context as you can see below:
Code:

from django.conf.urls.defaults import *
from mysite import views
from mysite.blog.models import Entry

blog_dict_welcome = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
'template_name': 'welcome.html',
}

blog_dict_detail = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
'extra_context': { 'fake_username': '' }
}


urlpatterns = patterns('',
(r'^$', views.home, blog_dict_welcome),
(r'^time/$', views.current_datetime),
(r'^time/plus/(\d{1,2})/$', views.hours_ahead),
(r'^admin/', include('django.contrib.admin.urls')),
(r'^search/$', 'mysite.books.views.search'),
(r'^contact/$', 'mysite.books.views.contact'),
#(r'^site_media/(?P.*)$', 'django.views.static.serve',
{'document_root': '/home/blah/Documents/web_design/djcode/
static_media'}),
(r'^blog/(?P\d{4})/(?P[a-z]{3})/(?P\w{1,2})/(?
P[-\w]+)/$', views.object_detail_wrapper, dict(blog_dict_detail,
slug_field='slug')),
#(r'^$', 'django.views.generic.date_based.archive_index',
blog_dict_welcome),
(r'^comments/', include('django.contrib.comments.urls.comments')),
(r'^accounts/login/$',  views.my_login_view),
(r'^accounts/logout/$', views.my_logout_view),

)

These are the other functions in the views.py file:

Code:

#require login for this page
@login_required
def home(*args, **kwargs):
return archive_index(*args, **kwargs)


@login_required
def object_detail_wrapper(*args, **kwargs):
kwargs['extra_context']['fake_username'] = alias['fake_username']
return object_detail(*args, **kwargs)

So, this all works fine on the development server on my laptop using
the latest django trunk, but on webfactions server it doesn't work all
of the time.  Sometimes when I access my object_detail( by the way I
also wrote my own freeform.html in templates/comments and modified it
to use the fake_username as the person_name) it shows the name and
then if I use Ctrl-R in firefox to refresh the page it shows nothing.
If I hit Ctrl-R again it puts my name back, and so on. It's like there
there are two copies of the global that I am accessing. I can't figure
this out.  Any ideas? Thanks so much!

csmithmaui

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---