Issue
I Tried importing app views into my url patterns but i get this error, using newest django version and python 3.11 and if it helps im using the anaconda navigator as a venv alternative
File "C:\Users\IK959\mysite\mysite\urls.py", line 18, in <module>
from chatapp.views import chat_view
ImportError: cannot import name 'chat_view' from 'chatapp.views' (C:\Users\IK959\mysite\chatapp\views.py)
this is my directory enter image description here
and this is my code for my url pattern and my view
urlpattern:
from django.contrib import admin
from django.urls import include, path
from chatapp.views import chat_view
urlpatterns = [
path("polls/", include("polls.urls")),
path("admin/", admin.site.urls),
path('chat/', chat_view, name='chat'),
]
chatapp view:
from django.shortcuts import render
def chat_view(request):
return render(request, 'chatapp/chat.html')
Ive tried the index but i do not know how to do it correctly
NEW EDITS:
This is my installed apps
Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'channels',
'chatapp',
]
urls.py app level
from django.urls import path
from django.shortcuts import render
from . import views
urlpatterns = [path('chat/', views.chat_view, name='chat'), ]
urls.py project level
from django.contrib import admin
from django.urls import include, path
from . import views
urlpatterns = [
path("chatapp/", include("chatapp.urls")),
path("admin/", admin.site.urls),
]
Solution
You have this
from chatapp.views import chat_view
path('chat/', chat_view, name='chat')
in the wrong place. To resolve this, move them from your project level urls.py
and add it to your apps urls.py
. In your case, it means you should move them from the urls.py file in mysite directory to the urls.py file in chatapp directory.
Also, I suspect you are following Django's polls tutorial. From the directory you showed in the link, your app has nothing to do with polls. So you might want to change this path("polls/", include("polls.urls"))
to path("chatapp/", include("chatapp.urls"))
in your project level urls.py.
EDIT based on additional requests from OP:
urls.py (project level)
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("chatapp/", include("chatapp.urls")),
]
urls.py (app level)
from django.urls import path
from . import views
app_name = 'chatapp'
urlpatterns = [
path('chat/', views.chat_view, name='chat'),
]
Answered By - journpy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.