I am trying to get my navigation to work in Django
This is what my about.html looks like
<ul class="dropdown-menu">
<li class="nav-item"><a class="nav-link" href="{% url 'courses' %}">Courses</a></li>
urls.py
from django.contrib import admin
from catalog import views
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('', views.about, name='about'),
path('', views.courses, name='courses')
]
and views.py
from django.shortcuts import render
#home page view
def index(request):
return render(request, "index.html")
#about page
def about(request):
return render(request,"about-us.html")
#courses page
def courses(request):
return render(courses, "courses.html")
As suggested by one of the answers, I made these changes to my urls.py file
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('courses/', views.courses, name='courses')
]
I am now getting this error message
AttributeError at /courses/
'function' object has no attribute 'META'
Request Method:
GET
Request URL:
http://127.0.0.1:8000/courses/
Django Version:
2.2.7
Exception Type:
AttributeError
Exception Value:
'function' object has no attribute 'META'
Exception Location:
C:\Users\User\Anaconda3\lib\site-packages\django\template\context_processors.py in debug, line 40
Python Executable:
C:\Users\User\Anaconda3\python.exe
Python Version:
3.7.4
Just update your urls.py to the following:
from django.contrib import admin
from catalog import views
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('courses/', views.courses, name='courses')
]
Related
I'm getting this attribute error when I am trying to import views from django.contrib.auth, and I wanna use their authentication method
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from django.contrib.auth import views
urlpatterns = [
url(r'', include('blog.urls')),
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout, name='logout', kwargs=
{'next_page':'/' }),
path('admin/', admin.site.urls),
]
AttributeError: module 'django.contrib.auth.views' has no attribute 'login'
url(r'^accounts/login/$', views.login, name='login'),
Django auth.view module doesn't have login function.
Try this (Django 2.2)
from django.contrib.auth.views import LoginView, logout_then_login, LogoutView
url(r'^accounts/login/$', LoginView.as_view(), name='login'),
url(r'^accounts/logout/$', LogoutView.as_view(), name='logout'),
or
url(r'^accounts/login/$', logout_then_login, name='login'),
I'm trying to use two slugs in my urls but I keep getting:
Reverse for 'tithe' with arguments '(2018, 'February')' not found.
1 pattern(s) tried: ['tithe/(?P<year>[0-9]{4})-(?P<month>[\\w-])/$']
urls.py
from django.contrib import admin
from django.urls import path,include,re_path
from django.conf.urls import url
from tithe import views
from django.views.generic import RedirectView
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(pattern_name="account_login"), name="index"),
path('accounts/', include('allauth.urls')),
path('dashboard', views.Dashboard.as_view(), name='dashboard'),
url(r'^tithe/(?P<year>[0-9]{4})/(?P<month>[\w-]+)/$', views.TitheView.as_view(), name='tithe'),
]
dashboard.html
<i class="zmdi zmdi-format-underlined"></i> <span> Tithe </span>
you can change urlpatterns like this:
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(pattern_name="account_login"), name="index"),
path('accounts/', include('allauth.urls')),
path('dashboard', views.Dashboard.as_view(), name='dashboard'),
path('tithe/2018/February/', views.TitheView.as_view()),
url(r'^tithe/(?P<year>[0-9]{4})/(?P<month>[\w-]+)/$', views.TitheView.as_view(), name='tithe'),
mysite/urls.py
from django.conf.urls import url,include
from django.contrib import admin
from django.contrib.auth import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('blog.urls')),
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout,
name='logout', kwargs={'next_page': '/'}),
]
blog/urls.py
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$',views.PostListView.as_view(),name='post_list'),
url(r'^about/$',views.AboutView.as_view(),name='about'),
url(r'^post/(?P<pk>\d+)/$', views.PostDetailView.as_view()
,name='post_detail'),
url(r'^post/new/$', views.CreatePostView.as_view(), name='post_new'),
url(r'^post/(?P<pk>\d+)/edit/$', views.PostUpdateView.as_view()
, name='post_edit'),
url(r'^drafts/$', views.DraftListView.as_view()
, name='post_draft_list'),
url(r'^post/(?P<pk>\d+)/remove/$', views.PostDeleteView.as_view()
,name='post_remove'),
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish
, name='post_publish'),
url(r'^post/(?P<pk>\d+)/comment/$', views.add_comment_to_post
, name='add_comment_to_post'),
url(r'^comment/(?P<pk>\d+)/approve/$', views.comment_approve
, name='comment_approve'),
url(r'^comment/(?P<pk>\d+)/remove/$', views.comment_remove
, name='comment_remove'),
]
The error occurs when I try to edit, remove the post, add a comment or remove comment from the post
I can't access any url from blog/urls.py
and I don't know where the error is coming from.
Error
In your template, you have a link where you have missed out the % in the URL tag. You have something like,
{ url 'post_detail' pk=post.pk }
but it should be:
{% url 'post_detail' pk=post.pk %}
When you click on the invalid link, it takes you to drafts/{url 'post_detail' pk=post.pk}, so you get the 404 error.
My mysite/urls.py is this
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And, my polls/urls.py is
from .import views
from django.conf.urls import url
from django.contrib.auth.views import login
urlpatterns= [
url(r'^$',views.index, name= "index"),
#127.0.0.1/polls
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name= "detail"),
#127.0.0.1/polls/1
url(r'^(?P<question_id>[0-9]+)/results$', views.results, name="results"),
#127.0.0.1/polls/1/results
url(r'^(?P<question_id>[0-9]+)/vote$', views.vote, name="vote"),
#127.0.0.1/polls/1/vote
url(r'^login/$', login, {'template_name': 'polls/login.html'})
]
The error I am getting is there is no registered namespace as polls. Do help.
In you mysite/urls.py file change this line:
url(r'^polls/', include('polls.urls')),
to this line:
url(r'^polls/', include('polls.urls', namespace='polls')),
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace="polls")),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
polls/urls.py should have
app_name = 'polls' #This line is missing
urlpatterns= [
url(r'^$',views.index, name= "index"),
#127.0.0.1/polls
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name= "detail"),
#127.0.0.1/polls/1
url(r'^(?P<question_id>[0-9]+)/results$', views.results, name="results"),
#127.0.0.1/polls/1/results
url(r'^(?P<question_id>[0-9]+)/vote$', views.vote, name="vote"),
#127.0.0.1/polls/1/vote
url(r'^login/$', login, {'template_name': 'polls/login.html'})
]
from django.shortcuts import render, HttpResponse
# Create your views here.
def index(request):
return render(request, 'platoweb/index.html')
def about(request):
return render(request, 'platoweb/about.html')
This is the views.py file.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index.html'),
url(r'^$', views.about, name='about.html'),
]
This is the app urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
url(r'^platoweb/index.html', include('platoweb.urls')),
url(r'^platoweb/about.html', include('platoweb.urls')),
url(r'^admin/', admin.site.urls),
# . url(r'^posts/', include("posts.urls", namespace='posts')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This is the global urls.py
Essentially, I'm trying to hit two different urls, index.html and about.html (and more later). However, when I run it, both index.html and about.html redirect to index.html.
I've tested both html files and they work just fine. Any ideas or thoughts?
First, you need change global urls.py like this:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
url(r'^platoweb/', include('platoweb.urls')),
url(r'^admin/', admin.site.urls),
# . url(r'^posts/', include("posts.urls", namespace='posts')),
]
Then your app urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about$', views.about, name='about'),
]
Then url http://127.0.0.1:8000/platoweb will go to your index page, and http://127.0.0.1:8000/platoweb/about will goes to your about page.