I'm new to django and had a question regarding organizing views. manage.py startapp creates a views.py in my app folder. But django-admin.py startproject <name> does not create a corresponding views.py file in the <project_name>/<project_name> folder.
I find it intuitive to have global views which do not correspond to a particular app. For example, a login page would and should be independent of any app that I create (its associated with the django auth app). So, would it make sense to create another views.py in the <project_name>/<project_name> folder where I can define such views?
(Just wanted to run it by experienced djangoers before I proceed.)
Thanks.
You can write your global views anywhere. it can be in any file name (I use, global_views.py)
I used to write a global to overrride/customize the default framework apps like custom authentication backend and custom sites.
Better to create a custom app and write all the global views.
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
Is it possible to do anything in the django-admin site? I mean can we custom it according to our needs??
django-admin is an application added to the default django projects. As you can see in settings.py file, there's 'django.contrib.admin' and 'django.contrib.auth' (which contains user management models and logics) in the INSTALLED_APPS array. You can get rid of them if you want and add your-own developed alternative apps instead. But django-admin is a powerful tool and there's many guides to custom it's functionalities. For example django admin cookbook is a famous one.
It's all dependent on what you want and need to do.
Yes of course, you can start copying whole /your_python_directory/site-packages/django/contrib/admin/templates/admin to your local template directory. (defined in your settings.py)
I am new to learning Django and I know how important setting up an application directory structure can be for future additions.
At the moment, I am creating an API using the Django REST Framework and have a few questions on how to structure a Django project for future success.
I want the API to feed out possibly in the future to other outside sources that may need to grab data. I also will be building a front-end CRUD like system to display and update data.
How would you suggest structuring the directories in the future possibility of adding more front-end like systems that are all powered by the Data API? Doing some research already, it seems like these are possibilities I have seen.
project
manage.py
project
settings.py
urls.py
api
models.py
serializers.py
views.py
crudapp
files here...
project
manage.py
project
settings.py
urls.py
api
models.py
serializers.py
views.py
crudapp
files here...
project
manage.py
project
settings.py
urls.py
crudapp
api
models.py
serializers.py
views.py
files here...
futureapp
api
models.py
serializers.py
views.py
files here...
I am really trying to understand how Django and Python should have these modules setup. If I have separate modules/apps setup, they can all access the models in the API app for any sort of app I build in the future?
Any clarification or experience on this would be greatly appreciated. The main goal is to have an API for the data in the database, and to then be able to build a front-end CRUD to update and pull that data, with the possibility that more front-end type apps could be built in the future as the data set grows.
For example, I am building a corporate data CRUD system to store important corporate information that will be accessible by a front-end and also an API that can feed to other non DJANGO websites. In the future, I may be adding an entirely different front-end/back-end CMS type system for a single page application. However, some of the data in the database from the corporate data set may be used to populate the website data.
i would take structure like this:
project
manage.py
apps
crudapp
futureapp1
futureapp2
so you can add as many apps as you wish and main project folder doesnot grow
and one more advantage is, your imports will be more beautiful and less name clashing in APPS:
from project.apps.crudapp.models import OneModel
# settings
INSTALLED_APPS = (
'project.apps.crudapp',
)
I want to add Django Sessions to my Django Admin, and I am following an SO post about this, but it is unclear where I store this code. Do I put it in an admin.py file? Under what directory?
In short, it doesn't matter. You can put the code into any of your apps' admin.py files. However, in situations like these I tend to use a generic app in my project, usually named something like utils, that exists for the sole purpose of housing code that doesn't belong to one specific app or could be used by multiple apps.
If you want to be more specific, you can create a sessions app in your project specifically devoted to this code and any other code related to session management for your project, or perhaps an existing app that is somewhat related. For example, I put customizations to the User admin in my accounts app that holds the UserProfile model.
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.