Having a little trouble with django-wsgi...

29 views
Skip to first unread message

Donnie Earnest

unread,
Aug 15, 2016, 4:01:41 AM8/15/16
to 2degrees FLOSS
I like the way the Django Project has integrated their site with Trac (code.djangoproject.com), and I've been unsuccessfully trying to accomplish that off and on for... well, longer than I care to admit.  I've tried making the two (Django and Trac) look the same so the users wouldn't notice but I can't get Trac to play nicely with bootstrap.  Then I tried iframes but that comes with a whole new set of headaches. Is django-wsgi the answer to my problems?  

I currently have (in production) Trac running stand-alone through mod_wsgi and Apache but when I bring Django in, I'm going to put Gunicorn and Tracd behind an Nginx reverse proxy. I've tested this in development and noticed a significant improvement in performance.

Anyway, I've tried following the instructions to implement this but I'm a little new to Python and WSGI and it's not going so well.  I currently have Trac (one instance, several repos) in a subdomain, for example trac.mysite.com, and Django in the root, www.mysite.com or mysite.com.  Based on your URLs example though, it looks like Trac should be in a subdirecty, like mysite.com/trac.  Is that the only way this will work?  

Also, I created a Django app for "code" (trying to follow Django's integration example) like mysite.com/code, which currently has an iframe pointing to code.mysite.com and I have my Django menu (bootstrap) at the top and a sliding menu on the left that has the Trac menu items (timeline, roadmap, browser, tickets, etc), so I just want to put the Trac content in the main area.  It looks great in the iframe like this but I'm not sure it's the best solution. I still have to deal with authentication.  My trac location is /var/www/trac and my django is /var/www/django, and I'm just using the system python version, 2.7, Fedora 22.  Since I was using Apache initially and Trac only works with python 2.x, I had to compile mod_wsgi with 2.x and then Django didn't work... yada yada yada.

Is this possible with django-wsgi or am I still going to have incorporate my own iframe solution for that seamless look and feel?  

Thanks in advance... and sorry for all the noob questions.
Donnie 

Gustavo Narea

unread,
Aug 15, 2016, 11:40:04 AM8/15/16
to 2degrees FLOSS

Hi there, Donnie.


It should be possible to run Trac on a separate domain, but it's a lot easier to run it on a separate path under the same domain as the Django app.


What you want to do is embed the Trac application inside your Django project, so that all requests to Trac go to a Django view in your project, which then passes them on to Trac. Here's a very old project where I integrated Trac in Django under the URL path "/trac":

https://bitbucket.org/Gustavo/weesgo

The main files you want to look at are:

https://bitbucket.org/Gustavo/weesgo/src/14fa15aadd15cdcfbb8042ae3a1a5d197c872faf/weesgo/views.py?at=default&fileviewer=file-view-default#views.py-21

https://bitbucket.org/Gustavo/weesgo/src/14fa15aadd15cdcfbb8042ae3a1a5d197c872faf/weesgo/urls.py?at=default&fileviewer=file-view-default#urls.py-12


You'll see references to "twod.wsgi", which is the predecessor to "django-wsgi".


I'd recommend that you make it work with the following steps:

  1. Make Trac work at "mysite.com/trac" by replicating what I did in the project above.
  2. Temporarily configure Nginx to serve your Django project from "mysite.com" and "trac.mysite.com". Trac would be accessible from "trac.mysite.com/trac". You might need to change some things in Django, like ALLOWED_HOSTS.
  3. Configure Nginx to map "trac.mysite.com/" to "127.0.0.1:port-number/trac". You then need to make Trac believe that it's hosted at "trac.mysite.com/" by setting the SCRIPT_NAME to an empty string.
In the end, your Django view should look like this (haven't tested it and you might need to update the import):

    from trac.web.main import dispatch_request as trac

    # (...)

    def run_trac(request, path_info):

        if path_info.startswith("/login"):
            response = redirect("/my-login-form")
        elif path_info.startswith("/logout"):
            response = redirect("/my-logout-form")
        else:
            request.environ['trac.env_path'] = "/opt/trac/env"
            request.environ['SCRIPT_NAME'] = ""
            response = call_wsgi_app(trac, request, path_info)

        return response

Hope that helps. Good luck! :)

 - Gustavo.


From: 2degree...@googlegroups.com <2degree...@googlegroups.com> on behalf of Donnie Earnest <don.e...@gmail.com>
Sent: 15 August 2016 03:47:37
To: 2degrees FLOSS
Subject: [2degrees-floss] Having a little trouble with django-wsgi...
 
--
You received this message because you are subscribed to the Google Groups "2degrees FLOSS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 2degrees-flos...@googlegroups.com.
To post to this group, send email to 2degree...@googlegroups.com.
Visit this group at https://groups.google.com/group/2degrees-floss.
For more options, visit https://groups.google.com/d/optout.

This e-mail, together with any attachments, is confidential and may contain copyright material of 2degrees Ltd. If you have received it in error please notify the sender immediately and delete the message from your system. You should not copy, distribute or use it for any purpose, or disclose the contents to any other person. Any views expressed in this message are those of the individual sender and may not reflect the views of 2degrees. Please contact us on +44 (0)1865 597640 if you need assistance.

2degrees Ltd.
Registered in England
Registered Office: 9400 Garsington Road, Oxford Business Park, Oxford OX4 2HN
Companies House Registered No. 06485099

Donnie Earnest

unread,
Aug 17, 2016, 4:23:14 AM8/17/16
to 2degrees FLOSS
Hi Gustavo, this was extremely helpful. Thank you very much!

I've configured everything as you've suggested and tried both putting Trac in a subfolder and a subdomain, and then I put it back as a subfolder since you said it would be easier and I just want to get it working... I have a tendency to over-complicate things...  I can access Django without any problems but as soon as I try to access Trac, I get "AttributeError: 'WSGIRequest object has no attribute 'webob'."

Could it be that I'm using a newer version of Django? 1.10?  I tried 1.4 but got a different error. 

Thanks again, I really appreciate your help.
Donnie

To post to this group, send email to 2degre...@googlegroups.com.

Gustavo Narea

unread,
Aug 17, 2016, 12:31:40 PM8/17/16
to 2degrees FLOSS

Hi, Donnie.


Did you integrate our WSGI handler as per http://pythonhosted.org/django-wsgi/request-objects.html ? That's what gives you "request.webob".


BTW, we haven't tested django-wsgi with Django 1.9 or v1.10 yet.


Cheers.


Sent: 16 August 2016 18:46:55
To: 2degrees FLOSS
Subject: Re: [2degrees-floss] Having a little trouble with django-wsgi...
 
To post to this group, send email to 2degree...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages