so I have created a Django application with three html pages. I want from a page which is not the homepage to another. However, the there seems to be some mix up in the url when the server tries to access the latter page.
Here's my application urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow.html/$',views.findnow,name='findnow'),
url(r'^more.html/$',views.more,name='more')
]
I wish to go from "findnow.html" to "more.html". I want the url to be "localhost:port/more" but instead the server goes to "localhost:port/findnow.html/more.html".
Here's my html code snippet for findnow:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
Here's my views.py:
def more(request):
return render(request,'myapp/more.html')
So adjust it to be:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow/$',views.findnow,name='findnow'),
url(r'^more/$',views.more,name='more')
]
And your template:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
This is happening because you are linking more.html as a relative page.
You should either prepend a / to it:
MORE
Or reverse it using the url tag instead:
MORE
Related
I started studying Django a few days ago and i want that when I click on a button it takes me to the url of my api.
This is my urls from my app:
from django.urls import path, include
from animes import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register('animes', views.AnimesViewSet, basename='animes')
router.register('nota', views.NotasViewSet, basename='notas')
urlpatterns = [
...
path('api/', include(router.urls), name='api')
]
the problem from my template is here:
<a class="botao__link" href="{% url 'api' %}"><button class="botao__botao" type="submit">API</button></a>
when i write the url manually i can go to the api without problems , but I can't go clicking the button
I'm not pretty sure but I think removing the type="submit" attribute should help
After several attempts I solved the problem by passing an absolute url
When I travel directly to the admin page of my django website by entering http://localhost:8000/admin/ in my address bar, the admin page is styled as it should be.
However, I need to create a link in my navigation bar that takes the user to the admin page but when the admin page is accessed from this nav link the normal admin styling disappears even though it appears to take me to the exact same web address.
[1]: https://i.stack.imgur.com/MrdOF.png
When I inspect the site it instead seems to be recieving styling from my jquery mobile stylesheet instead.
Any idea why this may be the case only when travelling to the admin address from the link?
EDIT:
Here is the html code for the nav. The result is the same regardless of using href='/admin/' or href="{% url 'admin:index' %}"
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'admin:index' %}">{% trans 'Staff Login' %}</a>
</li>
</ul>
Here are my urls.py files for each of my apps:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='form_page'),
path('search/', views.search_page, name='search_page'),
path('list/', views.list_skills, name='list_skills_page'),
path('skill/<slug:code>/', views.show_skill, name='show_skill_page'),
path('select/<slug:code_1>/', views.select_second, name='select_second'),
path('select/<slug:code_1>/<slug:code_2>/', views.view_second, name='view_second'),
path('language_preferences/', views.language_preferences_page, name='language_preferences_page')
]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('Generator.urls')),
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]
Also, I have noticed that when inspecting the source in my browser the admin static files do not appear in the page sources when viewing the admin site after clicking the anchor link (left) but are present if I directly enter the admin address in my search bar (right).
see here
SOLVED:
I found that some jquery js found in my base html template seemed to be interfering with the admin static files and stopping them from being served when I navigated to the admin site from the link in the navigation bar (no idea why!)
removing the following line fixed the admin style issue when navigating from the link:
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
so I simply wrote a javascript script to remove this jquery resource from the DOM when the link is clicked and the issue was resolved.
I'm new to coding, I'm trying to create URL link in NavBar to redirect my Django Admin exmple:
<a class="nav-link" href="{% url 'admin'%}"> Admin site </a>
However, Django do find; No-Reverse-Match,
It works for other URL links
Things I had to try:
Is adding a name in project URL
including URL path in my App URL
My project URL code:
from Django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(r'admin/', admin.site.urls, name='admin' ),
path(r'accounts/', include('django.contrib.auth.urls')),
path(r'api-auth/', include('rest_framework.urls'), name='rest_framework'),
path(r'', include('app.urls')),
My App URL code:
from django.urls import path, include
from . import views #function views
from django.contrib.auth.decorators import login_required, permission_required
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'tank', views.TankViewSet)
router.register(r'room', views.RoomViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path(r'',login_required(views.index), name='index'),
path (r'test/',views.Test),
path(r'^Water/',login_required(views.Lquid), name='tank'),
path(r'^Ambient/',login_required(views.Ambient), name='room'),
path(r'Rest-api/', include(router.urls)),
#path(r'admin/', admin.site.urls, name='admin' ), this somthing I had try
]
Basically, Django makes in a way that works and redirect to the URL link on any page
NAVBAR:
Admin site
App URL:
path(r'',login_required(views.index), name='index'),
With this two link it will redricet to the page you set.
Apprentice if you cold help
You need to specify the admin page you want to redirect to when doing a reverse lookup of admin. To get to the admin homepage you will want this reverse lookup
reverse('admin:index')
So to link to the admin homepage in your nav bar, it should look like this
<a class="nav-link" href="{% url 'admin:index' %}"> Admin site </a>
Check out the docs on how to reverse lookup admin pages
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' %}'
when I use django.contrib.auth.views.password_reset_confirm without arguments at all it works and I can render the template without any problem, when adding uidb36 and token arguments it fails.
Caught NoReverseMatch while rendering: Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{'uidb36': '111', 'token': '1111111111111'}' not found.
Most likely it is an issue with your urls.py. You need to setup the right pattern to grab the uidb36 and token values passed as URL parameters. If not, it will throw a similar error to what you see above.
Something like:
(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', {'template_name' : 'registration/password_reset.html', 'post_reset_redirect': '/logout/' })
registration/password_reset.html - is my custom template
logout - is my custom logout action
I had this issue in Django 1.3, and wasted a lot of time because the error can mask a number of underlying issues.
I needed to add this to the top of the reset email template:
{% load url from future %}
Also, the example in the Django docs didn't match the sample url:
{{ protocol}}://{{ domain }}{% url 'auth_password_reset_confirm' uidb36=uid token=token %}
So I had to change the auth_password_reset_confirm above to password_reset_confirm.
If you're using Django 1.6+ and run into something like this it could be that you need to update uidb36 to uidb64 in both your template and your urls.
Example url:
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm
and reset link in template:
{{ protocol}}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %}
For Django 1.8+ users, just copy this URL to your main urls.py file, so that it recognizes the URL name
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
'django.contrib.auth.views.password_reset_confirm',
name='password_reset_confirm'),
And add this mentioned by: #Lunulata to your password_reset_email.html file:
{{ protocol}}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %}
Try adding following to your urls.py
(r'^reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'django.contrib.auth.views.password_reset_confirm'),
I found this to work, copied from the default url
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
auth_views.password_reset_confirm, name='password_reset_confirm'),
Just add this line to your urls.py:
url('^', include('django.contrib.auth.urls')),
This enables the django reset_password workflow.
Then override your login.html to include the line:
<div class="password-reset-link">
href="{{ password_reset_url }}">{% trans 'Forgotten your password or username?' %}</a></div>
Now you should be able to use the builtin Django PasswordResetView included with Django as long as your email settings are set up.
if you are using app_name in every urls.py
suppose you have an app and in that app in urls.py you have mentioned app_name="accounts"
in order to retrieve the page you need to mention two things
template_name and success_url inside the PasswordResetView(template_name="accounts/password_reset.html" , success_url= reverse_lazy('accounts:password_reset_sent'))
dont forget to import reverse_lazy from django.urls inside urls.py
so your final code of accounts/urls.py should look like
My app name is landing instead of accounts
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
from django.urls import reverse_lazy
app_name='landing'
urlpatterns = [
path('',views.home,name="home"),
path('terms/',views.terms,name="terms"),
path('login/',views.loginUser,name="login"),
path('signup/',views.signupUser,name="signup"),
path('about/',views.about,name="about"),
path('logout/',views.logoutUser,name="logout"),
path('password_reset/',
auth_views.PasswordResetView.as_view(template_name='landing/password_reset.html',success_url=reverse_lazy('landing:password_reset_done')),
name="password_reset"),
path('password_reset_sent/',
auth_views.PasswordResetDoneView.as_view(template_name='landing/password_reset_sent.html'),
name="password_reset_done"),
path('reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='landing/password_reset_form.html',success_url=reverse_lazy('landing:password_reset_complete')),
name="password_reset_confirm"),
path('password_reset_complete/',
auth_views.PasswordResetCompleteView.as_view(template_name='landing/password_reset_done.html'),
name="password_reset_complete"),
]
you have to use app_name: before the name of url you have mentioned it is very important