url getting doubled when opening a link from the home page.(django) - django

On clicking the 2nd item in the list it opens the events.html page but the url shows:
http://127.0.0.1:8000/events/events/ instead of http://127.0.0.1:8000/events
Home page code:
<li>HOME</li>
<li>EVENTS</li>
<li>REGISTERED TEAMS</li>
<li>CONTACT US</li>
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home_page, name='home_page'),
path('hackathon_register/',views.hackathon_register,name='hackathon_register'),
path('events/',views.events,name='events')
]
views.py
def events(request):
return render(request,'testapp/events.html',{})

From here, it seems that your code in urls.py of project is:
.......
urlpatterns = [
....
path('events/', include("app.urls")),
]
So correct this mistake either by changing project.urls or app.urls.

Related

django path showing twice in url bar

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

How to call a template?

How can I call the the about.html from the following index.html? What to add to urls and views?
All the html files including about and index are collected in the project/static folder.
# This is part of the index.html, where I want to call the about.html
<div class="card-footer">
Learn More
</div>
# Here is the project/urls.py
from django.contrib import admin
from django.urls import path
from app1 import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
]
urlpatterns += staticfiles_urlpatterns()
If you are in one html file and want to open another html file you can put something like this in your html file:
Go to About
This will create a clickable link on the words Go to About. You can then position this anywhere you want on your page. In your urlpatterns you would need to make a new path like this
path('about', view.AboutView.as_view(), name='about')
Within your views.py you have to make a class called AboutView using templateView and put the name of your html file as template_name like this:
from django.views.generic.base import *
class AboutView(TemplateView):
template_name = "about.html"
Let me know if you need more help understanding this code or want to do something else
add about url in your urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('about/', views.about, name="about"),
]
add about views in your views.py file
def about(request):
# render your template here
in index.html
<div class="card-footer">
Learn More
</div>

How to navigate from current html page to another html page in django

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!

no module named search after adding project url

I've had the pleasure to work with somebody yesterday on the issue with my urls here Adding an additional template to an existing project errors out, but after trying everything suggested i'm still in the same situation.
My project is named mysite and my application is search.
It was suggested to add the following to my project urls.py
url(r'^search/', include('search.urls')),
When doing so I'm given the error ModuleNotFoundError: No module named 'search'.
My project urls.py is the following:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('search.urls')),
]
I'm attempting to add the following to my app urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
I have an empty view for results defined as
def results(request):
return render(request, 'results.html')
When I try to add the following POST to my form for the results it gives me the error in the first post. When I have the results url in my app.urls.py
<form action = "{% url 'results' %}" form method = "POST">
This is what my current application structure looks like. Please help get me on the right track. Thank you.
Your search directory is in your mysite directory (the one that includes settings.py. That means you should include mysite.search.urls (just as you use mysite.search in your import and INSTALLED_APPS).
from mysite.search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('mysite.search.urls')),
]
If your search directory was in your project directory (the one that includes manage.py, then you would remove mysite from the import, include() and INSTALLED_APPS.
from search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('search.urls')),
]

Django Linking a html page to a view

So I know that there are other people who have asked the same question, and I have read through them. However, the solutions provided there are giving me a strange error, and I would appreciate any help in understanding it.
So here's my home.html file:
<head>
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
<!-- Sign Up -->
Sign Up
</body>
And here's my views.py:
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, "home.html")
def signup(request):
return render(request, "signup.html")
And here's my urls.py:
from django.conf.urls import url
from .views import home, signup
urlpatterns = [
url(r'^signup/', signup, name="signup"),
url(r'^', home, name="home"),
]
Thank you for all the help :)
Edit:
The error message is
Reverse for 'signup' not found. 'signup' is not a valid view function or pattern name.
Also I actually changed the way I did urls.py. Now, I only have one urls.py in my main mysite folder:
from django.conf.urls import url, include
from django.contrib import admin
from home import views
from accounts import views as accountsViews
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home),
url(r'^signup', accountsViews.signup),
]
Your second url in 'urls.py' does not have a name.
The url tags would not be able to find them by name -- '{% url 'signup' %}'