JQuery .load() works in production but fails in development

98 views
Skip to first unread message

Joe Linoff

unread,
Jun 30, 2011, 9:36:28 PM6/30/11
to django...@googlegroups.com
Hi Folks:

I have to say that I am completely enamored with Django. I have been using it for about a week and it is a really awesome framework!

The problem I have is that JQuery .load() does not work correctly in my development environment on port 8001 (manage.py runserver 0.0.0.0:8001).

It works just fine in my production environment (port 80) which leads me to believe that the the problem is related to the "same origin" policy or some sort of Apache configuration problem but I don't know how to fix it.

I have a very simple example that illustrates what is going on. It is a page that dynamically call /cgi/getstatus.py script each time the "loadit" link is clicked.

How can I make it work in the development environment? Is there some way to allow apache to handle the CGI calls on port 8001 while Django runserver manages the rest? Is there a better way to do this?

Thank you,

Joe

Details follow.

Setup:
  O/S:      Linux (CentOS 5.5)
  Apache:   2.2.3
  Django:   1.3
  JQuery:   1.6.1
  mod_wsgi: 3.3.3
  Python:   2.7.2

CGI script:
  #!/opt/python2.7/bin/python2.7
  import cgitb
  import datetime
 
  cgitb.enable()
  now = datetime.datetime.now()
  print "Content-Type: text/plain;charset=utf-8"
  print
  print 'Timestamp %s' % (now.strftime("%Y-%m-%d %H:%M:%S"))

template (sync.html):
  {% extends "base.html" %}
  {% block head_title %}{{ title }}{% endblock %}
  {% block content %}
  <script type="text/javascript">
  $(document).ready(function() {
      getStatus();
  });
  function getStatus() {
      $('#status').load('/cgi/getstatus.py');
  }
  </script>
  <div id="status"></div>
  <a href="javascript:getStatus();"> loadit </a>
  {% endblock %}

views.py:
  from django.shortcuts import render_to_response
  from django.template import RequestContext
 
  def sync(request):
      return render_to_response('sync.html',
                                {'title':'JQuery .load() Test'},
                                context_instance=RequestContext(request))

httpd.conf:
  <VirtualHost *:80>
    ServerAdmin  m...@example.com
    ServerName   mysite-server.example.com
    DocumentRoot /opt/mysite/2.0/mysite
    ErrorLog     logs/mysite-server_error.log
    AddHandler   cgi-script .cgi .py
 
    ScriptAlias /cgi /opt/mysite/2.0/cgi
    <Directory /opt/mysite/2.0/cgi>
      Options Indexes FollowSymLinks
      Order allow,deny
      Allow from all
    </Directory>
 
    Alias /media /opt/mysite/2.0/media
    <Directory /opt/mysite/2.0/media>
      Options Indexes FollowSymLinks
      Order allow,deny
      Allow from all
    </Directory>
   
    Alias /static/admin /opt/python2.7/lib/python2.7/site-packages/django/contrib/admin/media
    <Directory /opt/python2.7/lib/python2.7/site-packages/django/contri/admin/media>
      Options Indexes
      Order allow,deny
      Allow from all
    </Directory>
 
    WSGIScriptAlias / /opt/mysite/2.0/mysite/apache/django.wsi
  </VirtualHost>


Joe Linoff

unread,
Jul 1, 2011, 1:28:11 AM7/1/11
to django...@googlegroups.com
Hi Folks:

I forgot to mention that FireBug tells me that development request has completed successfully (code 200) but there is no data which is what lead me to believe that it was a "same origin policy" problem possibly resulting from the use of the alternate port or from the fact that I just don't know how to tell runserver that /cgi is only for serving flat file executables.

Cheers,

Joe

Joe Linoff

unread,
Jul 1, 2011, 9:53:46 AM7/1/11
to django...@googlegroups.com
Hi Folks:

I figured it out. The idea is NOT to use CGI. It is not needed. Simply use a Django WSGI URL to access the python module (in a view) and everything it works fine. It as blindingly obvious once I figured it out.

Here is what I did to get it to work:
  1. Added a new URL called '/status/sync' in urls.py: url(r'^sync/status/$','status'),
  2. Added status to my views.py:
    from django.http import HttpResponse
    import datetime
    def sync_status(request):
        now = datetime.datetime.now()
        html = 'now = %s' % (now)
        return HttpResponse(html)
  3. Changed my index.html javascript as follows:
        $('#status').load('/employees/sync/status');
And everything worked perfectly in both environments. I have now removed the CGI stuff.

Sorry to have bothered you.

Regards,

Joe



Andre Terra

unread,
Jul 3, 2011, 11:13:08 PM7/3/11
to django...@googlegroups.com
I'm sorry none of us replied sooner; I for one completely missed the
original message.

For the record, you can use request.is_ajax to figure out whether a
request is coming from a js file and make the view behave accordingly.
It's mighty useful in some cases.

Also, have you gone trough the official tutorial? It seems to me there
are a few concepts you should be aware of.


Cheers,
Andre Terra

On 7/1/11, Joe Linoff <jli...@gmail.com> wrote:
> Hi Folks:
>
> I figured it out. The idea is NOT to use CGI. It is not needed. Simply use a
> Django WSGI URL to access the python module (in a view) and everything it
> works fine. It as blindingly obvious once I figured it out.
>
> Here is what I did to get it to work:
>

> 1. Added a new URL called '/status/sync' in urls.py:


> url(r'^sync/status/$','status'),

> 2. Added status to my views.py:


> from django.http import HttpResponse
> import datetime
> def sync_status(request):
> now = datetime.datetime.now()
> html = 'now = %s' % (now)
> return HttpResponse(html)

> 3. Changed my index.html javascript as follows:


> $('#status').load('/employees/sync/status');
>
> And everything worked perfectly in both environments. I have now removed the
> CGI stuff.
>
> Sorry to have bothered you.
>
> Regards,
>
> Joe
>
>
>

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/nSUGnywpdngJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Joe Linoff

unread,
Jul 4, 2011, 11:31:07 AM7/4/11
to django...@googlegroups.com
Hi Andre:

Thank you for responding and for your insights.


> For the record, you can use request.is_ajax to figure out whether a
> request is coming from a js file and make the view behave accordingly.
> It's mighty useful in some cases.

That is very useful.


> Also, have you gone trough the official tutorial?

I have gone through a number of tutorials from this page: http://docs.jquery.com/Tutorials. I have also gone through the Django tutorial (https://docs.djangoproject.com/en/1.3/intro/tutorial01/ ...). Is there a particular one that you recommend?

Regards,

Joe

Reply all
Reply to author
Forward
0 new messages