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'),
Related
I would like to set in Django a URL that will point to my index.html.
Now I could see at http://127.0.0.1:8000/micromundos but I would like to configure the url to show micromundos at http://127.0.0.1:8000/
This is my current myapp/urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('micromundos/', include("myapp.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This is myapp/myapp/urls.py:
from django.urls import path
from . import views
app_name = "micromundos"
urlpatterns = [
path("", views.index, name="index"),
]
Any help is appreciated. Thank you
Just omit micromundos in path('', include("myapp.urls")) from myapp/urls.py:
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("myapp.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
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')
]
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'),
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.