Dajngo - how set index page as http://127.0.0.1:8000/ - python

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)

Related

Having difficulties getting navigation to work in Django

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')
]

Using multiple slugs in urls.py

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'),

How to fix "NoReverseMatch at /polls/" with "'polls' is not a registered namespace" 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'})
]

django redirects different links to same page

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.

django registration redux URL's being effected by url with multiple query parameters

My Url's supplied by django registration redux don't work when the following url is used:
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
but then they do work if I add another level to that url like this:
url(r'^projects/(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
I don't want to add that extra project/ url level in there if possible. Why would it be effecting the django registration redux url's and how can I fix it? Any help would be much appreciated :-)
project urls
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from registration.backends.simple.views import RegistrationView
class MyRegistrationView(RegistrationView): #redirects to home page after registration
def get_success_url(self,request, user):
return '/register_profile'
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('howdidu.urls')),
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'), #redirects to home page after registration
(r'^accounts/', include('registration.backends.simple.urls')),
)
# media
if settings.DEBUG:
urlpatterns += patterns(
'django.views.static',
(r'^media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}), )
app urls
from django.conf.urls import patterns, url
from howdidu import views
from django.contrib.auth.decorators import login_required
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register_profile/$', views.register_profile, name='register_profile'),
url(r'^update_profile/$', views.update_profile, name='update_profile'),
url(r'^create_project/$', login_required(views.CreateProject.as_view()), name='create_project'),
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
)
The problem is your url ordering, take a look at this:
url(r'^admin/', include(admin.site.urls)),
url(r'', include('howdidu.urls')),
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'), #redirects to home page after registration
url(r'^accounts/', include('registration.backends.simple.urls')),
Especially this:
url(r'', include('howdidu.urls')),
this url matches everything, so basically Django will go from the top to the bottom and if it doesn't find any match before url(r'', include('howdidu.urls')), it will match anything to it. This means that you will never match
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'), #redirects to home page after registration
url(r'^accounts/', include('registration.backends.simple.urls')),
One way to fix it is to place url(r'', include('howdidu.urls')) at the very bottom of the urls:
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'), #redirects to home page after registration
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^', include('howdidu.urls')),
and another way is to set another url level for url(r'^', include('howdidu.urls')):
url(r'^admin/', include(admin.site.urls)),
url(r'^howdidu/', include('howdidu.urls')),
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'), #redirects to home page after registration
url(r'^accounts/', include('registration.backends.simple.urls')),

Categories