Home > Blockchain >  How do I import views.py to my project urls file
How do I import views.py to my project urls file

Time:11-30

I am working with Django and trying to do a login system

The app is supposed to deliver a simple 'Login System'-view but my_app/urls.py fails to import methods from my_app/views.py.

My app name is authentication

Here is my-project/urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('authentication.urls')),
]

Here is my_app/urls.py.

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

urlpatterns = [
    path('', views.home, name="home"),
    path("signup", views.signup, name="signup"),
    path("signin", views.signup, name="signin"),
    path("signout", views.signup, name="signout"),
]

Here is my-app/views.py

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

def home(request):
    return render(request, "authentication/index.html")

I have also added this in my-project/settings.py

'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR /"templates"],
        'APP_DIRS': True,

And I get the following error

TemplateDoesNotExist at /
authentication/index.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 3.2.9
Exception Type: TemplateDoesNotExist
Exception Value:    
authentication/index.html
Exception Location: C:\Users\julie\Login System\venv\lib\site-packages\django\template\loader.py, line 19, in get_template
Python Executable:  C:\Users\julie\Login System\venv\Scripts\python.exe
Python Version: 3.9.5
Python Path:    
['C:\\Users\\julie\\Login System',
 'c:\\python39\\python39.zip',
 'c:\\python39\\DLLs',
 'c:\\python39\\lib',
 'c:\\python39',
 'C:\\Users\\julie\\Login System\\venv',
 'C:\\Users\\julie\\Login System\\venv\\lib\\site-packages']

CodePudding user response:

The problem is about settings.py try it.

import os
'DIRS': [os.path.join(BASE_DIR, 'templates')]

CodePudding user response:

01) my-project/urls.py

urlpatterns = [
    path('', include('my-app.urls')),

    # .....
]

02) my-app/views.py

def home(request):
    return render(request, "authentication/index.html")

03) my-app/urls.py

urlpatterns = [
    path('', views.home, name="home"),

    # .....
]

(final) my-app (folder & files structure):

my-app:
  |-- templates
        |-- authentication
               |-- index.html
  |-- urls.py
  |-- views.py
  |...

nb: think simple

CodePudding user response:

Your views are in views.py within the module 'my_apps', am I right in assuming this..? If that is the case, you're to import views from within that module, and not the module 'authentication'.

In your my_app/urls.py, try to change the line,

from authentication import views

to

from . import views

and see if it works. The dot represents the directory containing urls.py, i.e. 'my_apps'.

  • Related