1

I am about to build a django project for a web app I am developing. For structural purposes, I want to know if it is possible to have two different apps within a project that communicate with each other like two different things in a single app within a django project. For example:

have two different parts of a django project. users and groups.

I currently have a testing project that has both within the general app of the django project. within the general project I included all parts of project in a single app. I want to know if i can break apart different parts of this project into seperate apps that can communicate with each other (templates, redirects, renders), and how I can connect them or what I need to do to connect them together.

example
  -example
    -__init__.py
    -settings.py
    -urls.py
    -wsgi.py
  -general
    -__init__.py
    -admin.py
    -apps.py
    -models.py
    -test.py
    -urls.py
    -views.p

THis is what I am thinking of doing

example
  -example
     -__init__.py
     -settings.py
     -urls.py
     -wsgi.py
   -users
     -__init__.py
     -admin.py
     -apps.py
     -models.py
     -test.py
     -urls.py
     -views.p
   -groups
     -__init__.py
     -admin.py
     -apps.py
     -models.py
     -test.py
     -urls.py
     -views.p

How can i connect the two different projects inthe urls file so I can redirect url's between the two. if possible, what would a redirect look like if I want to go from a url in the users app to a url in a groups app... What would the main urls file look like... in the example folder...

Another question is... are queries going to stay the same or do i need to change how queries are made...

3 Answers 3

3

Yes you certainly can have multiple apps communicate with each other in a django project.

example.urls:

from django.conf.urls import include, url

urlpatterns = [
    url(r'^users/', include('users.urls')),
    url(r'^groups/', include('groups.urls')),
]

users.urls:

urlpatterns = [

    url(r'^myurl/$', users.views.myview, name='users_myview'),

]

By using the name argument with an app identifier such as users_ you can ensure that your urls can be uniquely identified. In a view you can then refer to it as:

views.py:

from django.core.urlresolvers import reverse
reverse('users_myview',args=())
Sign up to request clarification or add additional context in comments.

Comments

2

You should read the Django documentation on URL dispatcher. Here is the part referencing inclusion of other URLconfs.

Include urls from apps in the urlpatterns in example.urls. I also suggest you use namespace argument to avoid confusion.

urlpatterns = [
    url(r'^', include('users.urls', namespace='users')),
    url(r'^', include('groups.urls', namespace='groups')),   
]

You then use url syntax namespace:urlname in views and templates.

Comments

2

groups: urls.py

urlpatterns = [
    url(r'^add-groups/$', view_1),
    url(r'^fetch-groups/group_id(?P<num>[0-9]+)/$', view_2),
]

users: urls.py

urlpatterns = [
    url(r'^add-users/$', group_add_view),
    url(r'^fetch-users/user_id(?P<num>[0-9]+)/$', group_fetch_view),
]

example: url.py (main url)

from django.conf.urls import include, url
from groups.urls import urlpatterns as group_patterns
from users.urls import urlpatterns as user_patterns
    urlpatterns = [
        url(r'^groups/', include(group_patterns)),
        url(r'^users/', include(user_patterns)),
    ]

/groups/add-groups/ will go to group_add_view

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.