问题
In the basic Django tutorials and documentations I have found so far, apps are treated as some standalone parts of the project. I haven't yet found, though, solutions of using them together in a complex project.
We can sign up at some url domain/signup
, and it is handled by users.views.signup
.
- For a GET request
signup
renderssignup.html
. - For a POST request, after evaluating the posted data, it either
renders
signup.html
with some message or registers a new user, logs him/her in and redirects somewhere.
We can create a new post at domain/new_post
(or domain/user/new_post
), which is handled by posts.views.new_post
. It acts similarly to the signup
handler:
- For a GET request it renders
new_post.html
. - For POST requests it evaluates post data and then either renders
new_post.html
with messages or registers the new post and redirects somewhere.
A general website is built up of several apps, and the webpage displayed for a request at some url offers functionalities from several apps.
For example, after logging into some popular social site, we can create new entries on our wall, search users (it is normally a wider search, but for sake of simplicity, let's only deal with users), also ads and our friends may be listed in the sidebars.
How are these parts built up from the different views and templates of the different apps?
To make the question more specific:
- For a GET request received at some url, how can we display
user-search-form, new-post-form and friends-list together?
If we do that by some project-level common view, what about the app-level views?
I have read that the{% include %}
tag is handy for building template from several "subtemplates", but what about the different variables the included templates take? - How can we handle user-search-related GET requests, and new-post-related POST requests that may be sent from the same url?
(The expression "requests sent from urls" may be inappropriate, but the user may send the different requests by acting on the page displayed at one specific url.) - Also, the different components should be kept decoupled.
Note: this question may be too generic for SO, so instead of detailed answers I would also appreciate sources, possibly with examples of using different apps together "in action".
回答1:
For a GET request received at some url, how can we display user-search-form, new-post-form and friends-list together?
You can request to render any template from any view. Django will search for the template through every registered app for a template
directory. Or you can specify your own template directories using TEMPLATE_DIR
, but it seems like you already knew this from your next question....
If we do that by some project-level common view, what about the app-level views?
Could you please try to explain this question in more depth?
I have read that the {% include %} tag is handy for building template from several "subtemplates", but what about the different variables the included templates take?
From the docs:
An included template is rendered with the context of the template that’s including it.
If you have a partial template, intended for including, like:
# hello.html
<h2>Hello {{ person.name }}</h2>
and another template is including it:
# greeting.html
{% include 'hello.html' %}
render_to_response('greeting.html', {})
the output will be Hello
because you do not have a person
in the template context
I don't quite understand your second bullet point. Your django package is just a python package. If you had two apps you can import code any way you choose, importing modules from your two different apps.
For example, in most projects I end up having a common
app. This usually contains utilities used throughout the whole django project. It usually contains my base test classes.
In your apps you can just import
from yourproject.common.utils import a_function
To help with decoupling django provides a signals framework. Your apps can emit and receive signals.
Django includes a “signal dispatcher” which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.
来源:https://stackoverflow.com/questions/15372251/django-using-different-apps-together-in-a-project