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.
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'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'),
When trying to click on a button to redirect to getonboard.html I get the following error:
django.urls.exceptions.NoReverseMatch: Reverse for 'getonboard' not found. 'getonboard' is not a valid view function or pattern name.
artdb/urls.py:
path('getonboard/',views.getonboard,name='getonboard'),
templates/artdb/base.html:
<p><a class="btn btn-primary btn-lg" href="{% url 'getonboard' %}" role="button">Get onboard ยป</a></p>
artdb/views.py:
def getonboard(request):
return render(request,'artdb/getonboard.html')
templates/artdb/getonboard.html:
{% extends "artdb/base.html" %}
{% block content %}
<h2>getonboard template</h2>
<p>this is the getonboard text</p>
{% endblock content %}
full main urls.py:
"""winmalist URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('artdb/', include('artdb.urls')),
path('admin/', admin.site.urls),
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
full artdb/urls.py:
from django.urls import path
from django.views.generic import TemplateView
from artdb.views import PersonList
from . import views
app_name='artdb'
urlpatterns = [
# path('base/',views.base,name='base.html'),
path('getonboard/',views.getonboard,name='getonboard'),
path('index/',views.index,name='index'),
path('persons/',PersonList.as_view()),
path('',TemplateView.as_view(template_name='artdb/base.html')),
path('test2',TemplateView.as_view(template_name='artdb/test2.html')),
# path('', views.IndexView.as_view(), name='index'),
# path('contract', views.contract, name='contract'),
# path('<int:person_id>/test1/', views.test1, name='test1'),
]
any thoughts?
When you include urlpatterns from a different app, you'll usually namespace those URLs by adding the line app_name = 'artdb' to the urls.py file inside the app. So, if the main urls.py file has the line:
urlpatterns = [
path('artdb/', include('artdb.urls')),
...]
And the artdb/urls.py file has these lines:
app_name = 'artdb'
urlpatterns = [
path('getonboard/', views.getonboard, name='getonboard'),
...]
then you can reverse this URL by namespacing the name:
reverse('artdb:getonboard')
This allows you to use the same name in multiple apps (e.g. index would be common), having artdb:index and user:index for example.
I've tried about a thousand different fixes suggested on different questions about this, but nothing seems to be working.
urls.py (app):
from django.contrib.auth.decorators import login_required
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', login_required(views.index), name='index'),
url(r'^clock/', views.clock, name='clock'),
]
urls.py (project):
from django.conf import settings
from django.contrib.auth import views as auth_views
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from mqtt.views import auth, acl, superuser
urlpatterns = [
url('^accounts/login/', auth_views.login,
{'template_name': 'login.html'}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^auth$', auth),
url(r'^superuser$', superuser),
url(r'^acl$', acl),
url(r'^$', include('lamp.urls')),
]
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
views.py:
from django.shortcuts import render
def index(request):
lamps = request.user.lamp_set.all()
context = {}
if lamps:
device_id = lamps[0].model
context = {'device_id': device_id}
return render(request, 'index.html', context)
def clock(request):
return render(request, 'clock.html')
index.html (snippet):
<div class="toggle-button" id="power"></div>
<div class="toggle-button" id="alarm-clock">
<a href="{% url 'clock' %}" id='clock-link'>Clock</a>
</div>
When I try to load the page I get:
NoReverseMatch at /
Reverse for 'clock' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
I'm pretty new to Django, so any help would be greatly appreciated.
$ means the end of urlconf search for django. you need to remove it to enable the inner-app urls search.
urlpatterns = [
url(r'^', include('lamp.urls')), # <----
#... rest of the urlconfs
]
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')),