I'm using django to make a little weather app for uni and I'm trying to link from the index page to a 'detail' page but in the url the path keeps apearing twice...
I've tried rewriting out the entire urls.py page (both of them). I've looked online as well but there dosent seem to be much about this (or I'm not looking in the right places)
so when I click on 'more info' for day 1 the url bar shows /day1/day1 instead of just /day1
[urls.py]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('weather.urls')),
path('day2/', include('weather.urls')),
path('day1/', include('weather.urls')),
]
[urls.py 2]
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('day1/', views.day1, name='day1'),
path('day2/', views.day2, name='day2'),
]
[views.py]
def day1(request):
return render(request, 'weather/day1.html')
def day2(request):
return render(request, 'weather/day2.html')
[link tag in main html page]
day 1
Related
code of project named carparts urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('parts.urls')),
]
code for my app named parts urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('about', views.about,name='about'),
path('contact', views.contact,name='contact'),
]
code for views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request, 'index.html')
# return HttpResponse('this is home page')
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
index page code output is show in http://127.0.0.1:8000 but my about and contact page shows Page not found (404) . Can any one help me with this code.
Thank you
Joint '/' in your both urls.py like that:
urlpatterns = [
path('', views.index,name='index'),
path('about/', views.about,name='about'),
path('contact/', views.contact,name='contact'),
]
According Django Design philosophy on Definitive URLs:
Technically, foo.com/bar and foo.com/bar/ are two different URLs, and
search-engine robots (and some Web traffic-analyzing tools) would
treat them as separate pages. Django should make an effort to
“normalize” URLs so that search-engine robots don’t get confused.
Using the URLconf defined in rldjango.urls, Django tried these URL patterns, in this order:
[name='index']
<int:destinations_id [name='destionations']
admin/
accounts/
^media/(?P<path>.*)$
The current path, destinations.html, didn't match any of these.
***"***from main urls.py******
urlpatterns = [
path('', include('travello.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('accounts.urls')),
]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
*from urls.py travello *
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index'),
path('<int:destinations_id', views.destinations, name='destionations')
views.py of travello
from django.shortcuts import render
from .models import Destination
# Create your views here.
def index(request):
dests=Destination.objects.all()
return render(request,"destinations/index.html",{'dests':dests})
def destinations(request):
return render(request, "index.html")
please tell what i am doing wrong
i am a newbii
i am wanting to add one more template to my app index.html is added and i am trying to add destinations.html to it
i am taking tutorials from telesko django course
please help
I'm working on django app and i'm facing a problem when i need to navigate from my index.html page to another about.html page. I'm setting everything in this way below:
urls.py (myproject)
from django.urls import path, include
urlpatterns = [
path('', include('weather_app.urls')),
path('about/', include('weather_app.urls'))
]
urls.py (myapp)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name="about")
]
In index.html, everything is working well, so i will not put the code.
and in view.py:
from django.shortcuts import render
import requests
def about(request):
return render(request, "about.html")
i have the code below in my index.html:
About
and i cannot get about.html page when click About link as you can see above.
Can anybody help me?
Thanks in advance..
You have to use this tag to avoid manual rendering of urls.
In your code:
About
Let me know if this works!
I have a few functions in the view.py in my Django project:
Here is the views.py and urls.py under polls:
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def search(request):
return HttpResponse("You're at the polls search.")
polls/urls.py
from django.urls import path
from . import views
from django.conf.urls import include, url
urlpatterns = [
path('', views.index, name='index'),
path('', views.search, name='search'),
]
I am able to get the index page, but have trouble to reach the page in the search function. I got the error below:
How do I access the search function in the views.py? Thanks!
edit your polls/urls.py as below:
urlpatterns = [
path('', views.index, name='index'),
path('search/', views.search, name='search'),
]
the first argument of the path is the url pattern.
I think you misunderstood the third argument(name). it has nothing to do with the url pattern, it's a name for the url, that'll be useful for url reversing. read the document for more information
I still new to Django and I'm following this tutorial in YouTube. I can't proceed to the next tutorial because I hit an error.
When I type http://127.0.0.1:8000/music. I get this error.
Check my code below:
mysite urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^music/', include('music.urls')),
]
music urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
music views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello World</h1>")
Please help.
You might have not saved the project/urls.py file.
Or to check whether the music is set in INSTALLED_APPS in settings.py?