views:

80

answers:

2

I have a django project set up with an app called pub. I'm trying to set it up so that I can include urls.py from each app (there will be more as I go) in the top-level urls.py. I've also got a template that uses the 'url' function to resolve a url on a view, defined in the openidgae module. The problem is that after the httprequest is routed to pub.views.index (like it's supposed to), I try to respond by rendering a template that uses the template 'url' function. The code I'm showing below is also here: http://gist.github.com/243158

Here's my top-level urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
   (r'', include('openidgae.urls')),
   (r'^pub', include('pub.urls')),  
)

and pub/urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
   (r'', 'pub.views.index'),
   (r'^/$', 'pub.views.index'),
)

and templates/base.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>{% block title %}My amazing site{% endblock %}</title>
</head>
<body>
    <div id="header">
     {% if lip %}
      Welcome {{ lip.pretty_openid }}
      <a href="{% url openidgae.views.LogoutSubmit %}">logout</a>
     {% else %}
     <form id="login-form" action="{% url openidgae.views.OpenIDStartSubmit %}?continue={{continueUrl}}" method="post">
      <input type="text" name="openid_identifier" id="openid_identifier" />
      <input type="submit" value="Verify" />
     </form>

     <!-- BEGIN ID SELECTOR --> 
     <script type="text/javascript" id="__openidselector" src="https://www.idselector.com/selector/46b0e6d0c8ba5c8617f6f5b970865604c9f87da5" charset="utf-8"></script>
     <!-- END ID SELECTOR -->
     {% endif %}
    </div>
{% block content %}{% endblock %}
</body>
</html>

and templates/pub/index.html:

{% extends "base.html" %}

{% block title %}blahblah!{% endblock %}

{% block content %}
  blahblahblah
{% endblock %}

and finally, pub/views.py:

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms
import openidgae

def index(request):
    lip = openidgae.get_current_person(request, HttpResponse())
    resp = render_to_response('pub/index.html', {'lip': lip})
    return resp

Now, if i set the second pattern in my top-level urls.py to point directly to 'pub.views.index', all works like it should, but not if I use the include function.

Any ideas? I'm sure the problem has something to do with the urlpattern that would map the views I'm trying to resolve to urls not being available to the template rendering functions when the HttpRequest is handled by the pub app rather than by the top-level, but I don't understand why or how to fix it.

+1  A: 

I don't understand what the problem is that you're facing, but just by looking at your urls.py files, you should probably change the top level urls.py to something like

from django.conf.urls.defaults import *

urlpatterns = patterns('',
   (r'', include('openidgae.urls')),
   (r'^pub/', include('pub.urls')),  
)

and pub/urls.py to:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
   (r'^$', 'pub.views.index'),
)

if you then use {% url pub.views.index %} somewhere in your templates, you should get the correct url.

Rishabh Manocha
A: 

You post a lot of code, but you haven't been very clear about what the actual problem is. First of all having

(r'', 'pub.views.index'),
(r'^/$', 'pub.views.index'),

can give some problems if you want to find the url. What should the url be for pub.views.index? From what you say, this is actually not be a problem, since you don't have a template tag that want to reverse the mentioned view. You don't actually say what is going wrong. But a way to fix the above problem, if you want to keep the two urls, would be to used named urls. I find it a bit redundant since you can just redirect example.com/pub to example.com/pub/, but you could transform the above to:

url(r'', 'pub.views.index' name='pub_a'),
url(r'^/$', 'pub.views.index', name='pub_b'),

Doing this you are now able to reverse your url, as you can uniquely identify them doing {% url pub_a %} or {% url pub_b %}. This would also make your templates easier to read, as you can give names that mean something, so it's easier to remember what's going on, while being more concise.

googletorp
Yep, I didn't edit my question very well. The problem is here: '{% url openidgae.views.LogoutSubmit %}'. I think I got it fixed, but I still don't quite have my head wrapped around how URLconfs really work. I know it's not that complicated, but it takes some time when you're just jumping in head first instead of studying the docs.
Ben Collins