Breaking Django views into seperate directories and files - django

I'm quite new to Django and I'm enjoying it a lot. I have broken my app's views into different files and placed them in a directory called app/views/(view files).
I have made an __init__.py file in the views directory this has caused me to have to use myproj.app.views.views in my site code. Which of coarse not very digestible.
Any ideas around this. Or is renaming my views directory to something else the way forward.
Thanks.

Just import the views from the other modules in __init__.py.

My user profile app account has three views files: views.py, views_login.py, and views_profile.py. Maybe it's not the cleanest, but it separates the three parts of account pretty well for my needs. My apps/account/urls.py therefore looks like this:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^foo1$', 'apps.account.views.foo1'),
(r'^foo2$', 'apps.account.views.foo2'),
(r'^bar1$', 'apps.account.views_login.bar1'),
(r'^bar2$', 'apps.account.views_login.bar2'),
(r'^baz1$', 'apps.account.views_profile.baz1'),
(r'^baz2$', 'apps.account.views_profile.baz2'),
)

Related

what is the use of creating a new way of url pattern in this code and what does it symbolize?

Blockquote
file path of the url
hello guys i am just a beginner in django so i cant understad what is the use of using this url pattern in this code
from django.conf import settings from django.conf.urls.static import static
urlpatterns += static(settings.MEIDIA_URL, document_root=settings.MEDIA_ROOT)
since user-uploaded content is assumed to exist in a production context, to see media
items locally we need to update config/urls.py to show the files locally. This involves importing
both settings and static at the top and then adding an additional line at the bottom.

How can I add new URLS to Django 3.2 under /admin?

I am working on upgrading an old Django project to 3.2. Previously, our urls.py for the main project included the following so that the urls from impersonate were below /admin
url(r"^admin/", include(admin.site.urls))
url(r"^admin/impersonate/", include("impersonate.urls")),
When I update this code to django 3.2, I don't seem to be able to include any urls beneath /admin. The following code:
re_path(r"^admin/", admin.site.urls),
re_path(r"^admin/impersonate/", include("impersonate.urls")),
does not work, but it does work if I modify the impersonate line to:
re_path(r"^impersonate/", include("impersonate.urls")),
Basically, I want to preserve all of the impersonate urls to be beneath /admin, if this is still possible. I understand that this does not make them require admin permissions, rather this is just to group all the admin views of the project together.
I have seen that I can write a custom ModelAdmin also, but this will still move the urls to under /admin/myapp/mymodel/my_view/. I do not want the extra part of the path mymodel here.
The solution to this was to reorder the paths:
re_path(r"^admin/", admin.site.urls),
re_path(r"^admin/impersonate/", include("impersonate.urls")),
Any URL patterns you define for custom admin views must occur before the admin patterns.
re_path(r"^admin/impersonate/", include("impersonate.urls"))
re_path(r"^admin/", admin.site.urls),

Changing URL pattern in Django so it doesn't show app name

I've got a folder/app named "projects" and there I want to make a new site there, named, let's say, "cheese".
In my urlpatterns in urls.py in projects folder I've got
path('cheese/', views.cheese, name='cheese')
but the whole URL looks like domain.co/projects/cheese
I'd like to omit that projects in URL, so it could just be domain.co/cheese
Those URL-things are kinda hard to understand for me and as I couldn't properly name my problem, I couldn't find a solution out there, though I believe there must be some.
The idea of having an app inside another app is kinda weird, but anyways, that won't enforce the inner app's urls to be like <outer_app>/<inner_app>/... if you set the url patterns correctly.
Basically, you've got domain.co/projects/cheese because, you include your project's app urls as:
# main urls.py file for your project
path('projects/', include('projects.urls'))
and in your project's urls file you have:
# projects/urls.py
path('cheese/', include('projects.cheese.urls'))
So if you want to have the cheese urls as domain.co/cheese, simply add the include to the main url file:
# main urls.py file for your project
path('projects/', include('projects.urls'))
path('cheese/', include('projects.cheese.urls'))

Is it possible to write my views in multiple files in Django?

The file "views.py" of my Django application has over 4000 lines of code right now, and it is difficult to scroll down every time to a specific view in my text editor.
I would like to divide this file up into multiple ones.
Is this feasible? I imagine that my "urls.py" would change accordingly. Would I still need to keep my "views.py" file?
Unlike models.py, there's nothing magical about the views.py file in Django. A view is simply a function referred to by a URLconf, and that can be in any module at all. Just make sure you refer to it properly, either by giving the full module as text, using the prefix, or importing it:
urls = patterns('',
(r'url1', 'app.views1.my_view1'),
(r'url2', 'app.views2.my_view2')
...
or
from app import views1, views2
urls = patterns('',
(r'url1', views1.my_view1),
(r'url2', views2.my_view2)
...
or even:
urls = patterns('app.views1',
(r'url1', 'my_view1'),
urls += patterns('app.views2',
(r'url2', 'my_view2'),
Sure, you can separate your views. You don't have to change your urls either. Just import the other file, and your original views file will inherit everything in the other file.
#views.py
from app.other_view import *
def your_view(request):
#your code here
#other_view.py
def your_view(request):
#your code here
There's ways to break up the default views.py, models.py, etc. files into modules of their own within the app, but I've found this usually ends up creating more problems than it solves.
In general, if you find the files too weighty, it's probably a better use of your time to refactor your code, instead. Do you really need all those views? Is there any way to simplify the logic? Would you benefit from breaking functionality out into an entirely separate app? These are the questions you should ask.

Google App Engine Project hierarchy

I'm working on a google app engine (with Django) and I just can't figure out what's a good practice for folder hierarchy.. I've looked at this: Project structure for Google App Engine
but one thing isn't clear - what if I have static folder (like js files) that are unique to my app, not project? where do they go?
my current hierarchy is:
proj
static
** js
** css
myapp
** templates
So when a template inside my app sends a GET for js/script.js. this gets redirected to /myapp/js/script.js, which my server doesn't recognize.
here is my project url.py:
urlpatterns = patterns('',
(r'^myapp/', include('myapp.urls')),
)
and here is my myapp.urls.py:
urlpatterns = patterns('myapp.views',
(r'^$', 'myapp.views.index'),
)
how should I rearrange this to work?
thanks!
Why not just use absolute URLs? Prefix your references to static resources with a /, and all will be well.