How many apps should I have in my Django project. For instance, I am building a pet application where there can be owners, pets, veterinarians, etc.
For now sticking to only the 3 I mentioned should I have these as separate apps within my Django project or should I just create one app to handle everything. I will be adding more features as time goes on.
The idea I had was to create a project
django-admin startproject mypets
Then create an application for each:
python manage.py startapp pet
and
python manage.py startapp pet_owner
and
python manage.py startapp veterianarian
As I mentioned before, there are other features I will be adding to my application like photos todo like feature and so on. My concern is that my project is going to get big which is why I want to separate pet_owner and pet each as their own apps. I was thinking this is exactly what I should do but then realized that when I went to mypets.urls.py file and I was thinking about forwarding some requests to a path I quickly realized that I want my route to always have mypets/.
urlpatterns = [
path('admin/', admin.site.urls),
path('mypets/petowner/', include('petowner.urls')),
path('mypets/pet/', include('pet.urls')),
]
but they seem separate in a way that I do not want them to be. For instance. I care about having mypets/petowner/<petowner-id>/pets/ etc. They are going to be very closely related to each other and I will be using relationships in my models. The separation that I want is mostly for organization and readability. I do not want to have a file models.py with a huge amount of code - I think that is ugly and messy.
I plan in the future to use React for my frontend, but because I'm trying to learn Django well I'm going to stick to a full Django app and use django-templates. Not sure if this matters but thought I would mention it just in case.
that is good idea and make project more organized specially for you
if you want to develope your code in the future this works and good for you
if you have problem with urls, now i have a suggestion, you can make a file like url.py in your app which contains your apps urls and then you can back to your main url and give it to urls it seems you know this way
first run your app and make it
now got to app folde and make a file like urls.py
now into that you can write your urls and write your urls in urlpattern as you know
before finish make sure that in the main url.py you have to include other urls that made
urlpatterns = [
path('', include('petowner.urls')),
path('admin/', admin.site.urls),
]
maybe this is good to change your names if involve you too much and write your urls in that app by this way you can be more organized and give your prefix or anything just in your app/urls.py
Related
Almost every video which I saw about Django (for beginners), people who create applications using the startapp command and add their urls.py file manually in their application. My question is, if urls.py is so important for views and for our app why it's not creating automatically when we run startapp command!
Not every app directly serves the end user
URLs.py is only useful for routing users to pages which primarily have to do with that app. However, many apps may only do internal things. I have an app in one of my projects that handles badges and rewards, but there is no page which corresponds to any of that because it all shows exclusively as part of the profile pages (and the routing is handled within the profile app).
It just isn't always needed and that is why it is not always included.
Simply you don't have to serve each of your app to the end-users. You may have apps responsible for only your inner interactions. So it is not logical to put urls.py in each and every app.
It vary on how you use your routing.
django give project wide urls.py by default when you create the project using django-admin startproject command. so you can create all your project's urls on this file.
And not all app intended to server user directly using urls.
Whether i also prefer to create separate urls.py and api-urls.py routers for every app and include in main urls.py
I am curious if it is possible to hide sphinx documentation inside a django app so that only people who log in can see it. It seems to me that since sphinx creates its own structure and that Django uses the urlconf to define which pages a user can see, that it wouldn't be possible to combine the two. Although there must be some combining since the Django website likely uses django and sphinx. I am wondering if anyone has any insight or if they can point me in the right direction.
Thank You in Advance!
Sphinx builds your docs into HTML files, so in most cases this docs should be served by your web server rather then Django. However Django is able to serve static files as well.
You can use the django.views.static.serve function to do this and wrap this function with login_required. E.g:
from django.views.static import serve
from django.contrib.auth.decorators import login_required
urlpatterns += patterns('',
url(r'^docs/(?P<path>.*)', login_required(serve), {'document_root': '/path/to/sphinx/build/html'}, 'docs'),
)
However this configuration will be considered a bad practice in production environment as in this case Django will serve both html and css/js files from your sphinx theme.
The first improvement you can do here is to serve /path/to/sphinx/build/html/_static/ with apache/nginx or whatever you use.
The more proper way is to serve docs with apache/nginx and make it handle the auth itself. Unfortunately I made a quick Google search but did not find a way to use Django's User model to handle http_auth in apache or other. Alternatively you can use something like mod_sendfile or X-Accel modules - http://www.wellfireinteractive.com/blog/nginx-django-x-accel-redirects/ In a nutshell - Django app checks permission if user can view the file and add special header to response containing file path. Webserver will serve this file instead of original message from django
I just need some advice pointing me into the right direction using Django with multiple sites / clients.
Basically depending on the domain name I want to use multiple sites with only one Django instance.
For example a directory structure.
mysite/
manage.py
settings.py
client1/
url.py
client2/
url.py
So what I am thinking is, inside settings.py depending on the domain name I can change the
ROOT_URLCONF = 'mysite.clientx.urls'
The site will match about 95% so I don't see the point of changing all other settings as well.
How would I do this? I did go through site management in the Django documentation although it seems like an overkill for what I want to accomplish.
Also keep in mind I am using Apache with "django.wsgi".
Best case scenario: Just modify the urlpatterns of one of them to include the urlpatterns of the other.
But as of now they both have seperate settings.py, seperate DB's, seperate directories. I assume I may have to somehow merge their two settings.py, include one of them in the other's INSTALLED_APPS, and resolve a bunch of directory issues. Can I somehow just have one invoke the other through urls.py and forego all the above. Any website documentation covering all this in detail, that's the main thing. Sorry if this has been asked. The problem is the existing Django project is running under one uwsgi process on the server, and adding another uwsgi process bumps it up to another account level.
Not sure if this is what you mean by "invoke the other through URLs.py", but you can include URLs from one app within another very easily. For example:
urlpatterns = patterns('', url(r'^polls/', include('polls.urls')))
That will include all the URLs in the polls app. You can read more on it here:
https://docs.djangoproject.com/en/dev/intro/tutorial03/#decoupling-the-urlconfs
This is state of my knowledge now. Something like uwsgi and I guess other server schemes ask for the specification of a single settings.py as a parameter. So as far as integrating multiple Django projects in a single uwsgi process, they will have to share a single settings.py. The only problem there is only one setting for MEDIA_URL and MEDIA_ROOT. And the only solution I see is putting the media from both projects in one folder - seems unreasonable. Am I missing something.
I am creating a Django based app and I'd like to put everything under the root in the following structure:
/path/to/my/app/
settings.py
models.py
urls.py
admin.py
...
One problem that I run into is the admin interface doesn't include whatever models I have that are registerd in admin.py usin
admin.site.register(models.MyModel)
Usually that's done by using auto discover in urls.py, but now I have no registered "app", the auto discover doesn't work anymore. Is there anyway I can still use the admin interface?
Thanks.
Django simply doesn't work without apps. They're the fundamental building block of a Django site. A whole range of things, not just the admin, will fail to work. Why do you want to do this?
Putting the app in the django-style directory structure will make your project easily extensible if you decide to add functionality later.