I have been trying twice now to add a second application onto my Django-site, whereas it results in some kind of errors.
I have followed instructions from youtube which has been of great help but now I am stuck at adding a second page. My first one is working just fine.
This is my main url.py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^table/', include('table.urls')),
url(r'^', include('login.urls')),
]
This is my main settings.py:
INSTALLED_APPS = [
'login',
'table',
....
TEMPLATES = [
{
'BACKEND': 'django.templates.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.templates.context_processors.debug',
'django.templates.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
This is my working page url.py:
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
This is my working page view.py:
from django.shortcuts import render
def index(request):
return render(request,'login/login.html', {'content': ['username',
'password']} )
This is my non-working page url.py:
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.index, name = 'index'),
]
This is my non-working page view.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request,'table/table.html')
Thus far I have thought of the index(request) being an issue since they are both having the name "view" and same function name...?
And I have no idea where to look on the "error-page" nor what to show you guys, I'm sorry. And I appreciate any help. Thank you.
"During handling of the above exception ('django'), another exception occurred:
C:\python36\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request) ...
▼ Local vars
Variable Value
exc
ModuleNotFoundError("No module named 'django.templates'",)
get_response
>>>
>
request
"
EDIT: I have named all my template-folders templates, always. Though I mistakenly named it without an s while creating it inside the second application but it is changed by refactoring.
I think this is my error now:
Exception Type: ModuleNotFoundError at /
Exception Value: No module named 'django.templates'
The error message is stating you are referring to django.templates somewhere. This module does not exist in django, but django.template does. You can find several django.template.x statements in your settings.py file.
Replace django.templates.x with django.template.x and you are good to go!
In your urls.py, specify the app_name like this:
# ...imports...
app_name = 'tables'
urlpatterns = [
#...
]
The name of the url can then be accessed by your template by tables:index
Related
No matter what I try in vs code the request is always greyed out. I'm following the Django tutorial and I get a 404 when loading the server because the index function can't be called cause the parameter is greyed, This works perfectly fine in Pycharm, but not vs code. Any solutions?
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
this is the urls for polls
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
And urls for the whole website
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
The grayed out 'request' parameter in your function does not mean that it isn't called, it's telling you that the parameter isn't used, also make sure you use indentation in your function views:
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
I always get this error "The empty path didn't match any of these." When I try to access the page through this url:
url('^about/$',views.AboutView.as_view(),name = 'about')
and when I remove "^about/$" part, then it works:
url('',views.AboutView.as_view(),name = 'about')
How could I resolve it?
This is link for call:
<li><a class="navbar-brand" href="{% url 'about'%}">About</a></li>
this is view.py
class AboutView(TemplateView):
template_name = 'about.html'
and, this urlpatterns
urlpatterns = [
url('^about/$',views.AboutView.as_view(),name = 'about')
]
from django.conf.urls import url
from blog import views
urlpatterns = [ url('about',views.AboutView.as_view(),name='about') ]
instead of this
from django.urls import path
from blog import views
urlpatterns = [
path('about/', views.AboutView.as_view(),name='about'),
use this pattern same as your main url
path('about/', views.AboutView, name='about'),
It's not good to follow 2 ways of creating urls, Since django==2.0 they have introduced very nice and easy way to declare urls.
In the old way...
from django.conf.urls import url
urlpatterns = [
url(r'^about/$', AboutView.as_view(), name="about")
]
But In the new way it's lot more cleaner...
from django.urls import path
urlpatterns = [
path('about/', view.AboutView.as_view())
]
But if you want to stick with the regular expressions, Use re_path() instead of path().
urlpatterns = [
re_path(r'about/$', view.AboutView.as_view())
]
In my it's better stay with one pattern, old or new but not both. It makes your code look more cleaner.
i make a startapp "guestbook" inside my django project.
here is the file list(guestbook)
__init__.py admin.py apps.py migrations models.py template tests.py urls.py views.py
guestbook/template
guestbook
guestbook/template/guestbook
index.html
i want to get the index render but i am facing error which is
File "C:\Users\_monster\Desktop\skill\django_frontend\backend\f_django\guestbook\urls.py", line 7, in <module>
path('', views.index, name='index')
AttributeError: module 'guestbook.views' has no attribute 'index'
here is my file setting
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
# main url, folder url
path('admin/', admin.site.urls),
path('hello/', include('hello.urls')),
path('guestbook/', include('guestbook.urls'))
]
guestbook/urls.py
from django.urls import path
# import everything from views
from . import views
urlpatterns = [
path('', views.index, name='index')
]
guestbook/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'guestbook/index.html')
so what is problem here ? the index.html are inside the folder
I had the same error. I Tried almost everything to solve the issue but couldnt.
I just changed the name of function index from views.py to index2(guestbook/views.py)in your case!! Then i replicated those changes to (guestbook/urls.py) as
urlpatterns = [
path('', views.index2, name='index')
]
It happened miraculously. And the console showed no errors anymore!! When i switched back again to whatever previously was, (that is to index from from index2) everything was running correctly
I have created this app in django but i cannot access the app . CMD says
Attribute error : module 'hello.views' has no attribute 'index'
VIEWS.PY
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World !!!')
URLS.PY/admin
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello.urls'))
]
URLS.PY/Hello
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.index, name='index')
]
You need to put commas after path().
path('hello/', views.index, name='index'), # Very important comma.
I think you missed one in each file.
I now see the other problem. Change your hello.urls.py path to
path('',
Your current configuration is looking for
/hello/hello/
If you put that in your browser it might work.
I'm following all of the instructions in this tutorial, time 2:26
https://www.youtube.com/watch?v=gqRLPx4ZeSw&t=3s
and I cannot get the expected result. The TypeError I'm getting is
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
File: urls.py
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', 'posts.views.post_home'),
# url(r'^posts/$', '<appname>.views.post_home'),
]
File: views.py
from django.shortcuts import render
from django.http import HttpResponse
def post_home(request):
return HttpResponse("<h1>Hello</h1>")
And here are the relevant screenshots, however I cannot post them because the computer thinks that they're code. Because it thinks they're code when I hit cntrl k the screenshots go away, but if I do not hit cntrl k, then I cannot post the thread.
You should do this for your code to work:
from posts import views as posts_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', posts_views.post_home),
]
But it's best you use include to append the app urls, you have to create an
urls.py in your posts app.
from django.conf.urls import url, include
url(r'^posts/', include('posts.urls')),