Prechádzať zdrojové kódy

Fixes django tutorial

Ask Solem 11 rokov pred
rodič
commit
21028bda40

+ 22 - 23
docs/django/first-steps-with-django.rst

@@ -33,24 +33,7 @@ that defines the Celery instance:
 
 :file: `proj/proj/celery.py`
 
-.. code-block:: python
-
-    from __future__ import absolute_import
-
-    import os
-
-    from celery import Celery
-    from django.conf import settings
-
-    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
-
-    app = Celery('proj')
-    app.config_from_object('django.conf:settings')
-    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
-
-    @app.task(bind=True)
-    def debug_task(self):
-        print('Request: {0!r}'.format(self.request))
+.. literalinclude:: ../../examples/django/proj/celery.py
 
 Then you need to import this app in your :file:`proj/proj/__init__py`
 module.  This ensures that the app is loaded when Django starts
@@ -58,11 +41,7 @@ so that the ``@shared_task`` decorator (mentioned later) will use it:
 
 :file:`proj/proj/__init__.py`:
 
-.. code-block:: python
-
-    from __future__ import absolute_import
-
-    from .celery import app
+.. literalinclude:: ../../examples/django/proj/__init__.py
 
 Note that this example project layout is suitable for larger projects,
 for simple projects you may use a single contained module that defines
@@ -134,6 +113,26 @@ Finally, the ``debug_task`` example is a task that dumps
 its own request information.  This is using the new ``bind=True`` task option
 introduced in Celery 3.1 to easily refer to the current task instance.
 
+Using the ``@shared_task`` decorator
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+The tasks you write will probably live in reusable apps, and reusable
+apps cannot depend on the project itself, so you also cannot import your app
+instance directly.
+
+The ``@shared_task`` decorator lets you create tasks without having any
+concrete app instance:
+
+:file:`demoapp/tasks.py`:
+
+.. literalinclude:: ../../examples/django/demoapp/tasks.py
+
+
+.. seealso::
+
+    You can find the full source code for the Django example project at:
+    https://github.com/celery/celery/tree/3.1/examples/django/
+
 Using the Django ORM/Cache as a result backend.
 -----------------------------------------------
 

+ 2 - 0
examples/django/demoapp/tasks.py

@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
 from celery import shared_task
 
 

+ 1 - 1
examples/django/proj/__init__.py

@@ -2,4 +2,4 @@ from __future__ import absolute_import
 
 # This will make sure the app is always imported when
 # Django starts so that shared_task will use this app.
-from .celery import app
+from .celery import app as celery_app

+ 1 - 1
examples/django/proj/celery.py

@@ -19,4 +19,4 @@ app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
 
 @app.task(bind=True)
 def debug_task(self):
-    print(repr(self.request))
+    print('Request: {0!r}'.format(self.request))