I have a project which has just assimilated several apps from other projects of mine to one main project. The structure is one main app that lies at the root url and other apps with specific urls. I have set up my urls.py:
url(r'^$', include('main_app.urls')),
url(r'^app1/', include('app1.urls')),
url(r'^app2/', include('app2.urls')),
url(r'^app3/', include('app3.urls')),
I have a model in my main_app models.py which describes my other apps along the lines of:
class App(models.Model):
title = models.CharField()
image = models.ImageField("App Image", upload_to="images/apps/", blank=True, null=True)
description = models.TextField()
And in my main_app views:
def index(request):
app_list = App.objects.all()
return render_to_response('index.html',
locals(), context_instance=RequestContext(request))
So, in my root index template (main_app) I want to cycle through all apps and print a link:
{% for app in app_list %}
{{ some_app_variables }}
<a href="???">Link to app</a>
{% endfor %}
My question is how should I define this link. Should I have a get_absolute_url for the app model?
Any help much appreciated.
An app doesn’t inherently have some URL associated with it. URLs are tied to views, and those views could reference any model from any app or no models or apps at all for that matter. What’s you’re doing doesn’t make sense.
UPDATE: I’m still unsure about what you’re linking to. An “app” is an abstract concept. I understand having a list of “Apps”, but what kind of view would you get for an individual app? Still, yes, you should have a
get_absolute_urlmethod on yourAppsmodel. Then, your best bet would be to name whatever view in each app is going to be the “index” view something along the lines of “app_(title)”. Then you can construct the right reverse with something along the lines of:Of course, you should probably modify that with something akin to a slug to accommodate titles that might have multiple words, e.g. “Cool App” would need to be replaced with something like “cool_app”.
Like I said, though, what you’re trying to accomplish still doesn’t make much sense from where I’m sitting.