Django views in project directory
Asked Answered
M

4

10

I have a project with two apps with their respective views, but I want to create a views.py in django project directory for some generic pages such as about, login... It is correct to place the views.py and templates in the root project folder for this purpose?

Memorial answered 1/12, 2016 at 15:31 Comment(2)
I believe it is correct. I saw many projects where a views.py file exists in the same level as settings.py and urls.pyCellulosic
This may be a relevant discussion: https://mcmap.net/q/118324/-django-quot-projects-quot-vs-quot-apps-quotFlax
C
2

I have been reading about this. The practice more recommended for this is create an app that creates cohesion between other apps, call it web_site or site whatever is better for you.

python manage.py startapp my_site 

Everything in this site app will be done the regular way. In the projects url you will want to import the url in this way so the web pages appear in the / url pattern.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('my_site.urls'))
]
Cough answered 13/10, 2019 at 18:5 Comment(0)
F
1

I do this for project level pages like you describe. These are usually simple pages that tie the project's apps together, and not contain any business logic.

Faux answered 1/12, 2016 at 18:41 Comment(0)
T
1

This question seems like a matter of opinion, but my take is: as long as the "generic" pages are quite simple, it is perhaps better to implement them in the root project directory. Actually, if the web pages are templates that don't need special context data, you can even do away with a views.py and just add something like this in your root urls.py:

path("", TemplateView.as_view(template_name="home.html"), name="home")

While @jogarcia's answer argues for a separate app in the name of cohesion, I think there's a case to be made that keeping these simple, generic pages in the "root" project directory actually makes for more cohesive code, because they are quite basic and central to the project like settings.py. The pages tend to be linked to the other apps the same way as the root urls.py too, so it shouldn't increase coupling.

If the "generic" pages start getting a lot of complex functionality though, then the separate-app option might become more attractive.

Tuft answered 28/6, 2023 at 5:32 Comment(0)
H
0

You can do that by making views.py file into the project directory.

from django.contrib import admin
from django.urls import path, include
from attendance import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('insert.urls')),
    path('login/', views.logins, name='login'),# relevant line
]

And this is the code of login view

from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.


def logins(request):
    return HttpResponse("THIS IS LOGIN PAGE !!!")
Horodko answered 22/1, 2020 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.