Django 在应用程序之间引用 url

发布于 2024-12-13 06:55:12 字数 1016 浏览 3 评论 0原文

我有一个项目,它刚刚将我其他项目中的几个应用程序吸收到一个主要项目中。该结构是一个位于根 url 的主应用程序和具有特定 url 的其他应用程序。我已经设置了我的 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')),

我的 main_app models.py 中有一个模型,它描述了我的其他应用程序:

class App(models.Model):
    title = models.CharField()
    image = models.ImageField("App Image", upload_to="images/apps/", blank=True, null=True)
    description = models.TextField()

在我的 main_app 视图中:

def index(request):
    app_list = App.objects.all()
    return render_to_response('index.html',
            locals(), context_instance=RequestContext(request)) 

所以,在我的根索引模板(main_app)中,我想循环遍历所有应用程序并打印链接:

{% for app in app_list %}
    {{ some_app_variables }}
    <a href="???">Link to app</a>
{% endfor %}

我的问题是我应该如何定义此链接。我应该为应用程序模型提供 get_absolute_url 吗?

非常感谢任何帮助。

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'^

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.

, 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:

And in my main_app views:

So, in my root index template (main_app) I want to cycle through all apps and print a link:

My question is how should I define this link. Should I have a get_absolute_url for the app model?

Any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-12-20 06:55:12

应用程序本质上没有与其关联的 URL。 URL 与视图绑定,这些视图可以引用任何应用程序中的任何模型,也可以根本不引用任何模型或应用程序。你在做什么没有意义。

更新:我仍然不确定您链接到的内容。 “应用程序”是一个抽象概念。我知道有一个“应用程序”列表,但是对于单个应用程序您会得到什么样的视图?不过,是的,您的 Apps 模型上应该有一个 get_absolute_url 方法。然后,最好的选择是将每个应用程序中的任何视图命名为“索引”视图,类似于“app_(title)”。然后,您可以使用以下内容构建正确的反向内容:

@models.permalink
def get_absolute_url(self):
    return ('app_%s' % self.title, (), {})

当然,您可能应该使用类似于 slug 的内容对其进行修改,以适应可能包含多个单词的标题,例如“Cool App”需要替换为类似的内容“酷应用程序”。

但就像我说的,从我的角度来看,你想要完成的事情仍然没有多大意义。

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_url method on your Apps model. 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:

@models.permalink
def get_absolute_url(self):
    return ('app_%s' % self.title, (), {})

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文