%5E being inserted into reverse url - django

I'm having a problem with a url resolving as it should. It's resolving as
http://localhost:8000/%5Ewebsites/?value=1&id=1 when it should be resolving as
http://localhost:8000/websites/?value=1&id=1
I've got the following urls.py inside of an app.
app_name = 'websites'
urlpatterns = [
url(r'^$', website_views.homepage, name="homepage"),
url(r'^blog/$', website_views.blog, name="blog"),
url(r'^blog/(?P<id>\d+)/$', website_views.blogpost, name="blogpost"),
]
I've got this inside of my project urls.py file:
urlpatterns = [
url(r'^websites/', include('websites.customerurls')),
The link that is resolving incorrectly is this:
<a href="{% url 'websites:homepage' %}?value=1&id=1" target="_blank">
Any help would be appreciated! Thanks!

I have to delete the ^ in urlpatterns = [ url(r'^websites/', include('websites.customerurls')),. It now resolves without the %5E and the page loads correctly.

Related

url getting doubled when opening a link from the home page.(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.

Path is not match any of these

I always get this error "The empty path didn't match any of these." When I try to access the page through this url:
url('^about/$',views.AboutView.as_view(),name = 'about')
and when I remove "^about/$" part, then it works:
url('',views.AboutView.as_view(),name = 'about')
How could I resolve it?
This is link for call:
<li><a class="navbar-brand" href="{% url 'about'%}">About</a></li>
this is view.py
class AboutView(TemplateView):
template_name = 'about.html'
and, this urlpatterns
urlpatterns = [
url('^about/$',views.AboutView.as_view(),name = 'about')
]
from django.conf.urls import url
from blog import views
urlpatterns = [ url('about',views.AboutView.as_view(),name='about') ]
instead of this
from django.urls import path
from blog import views
urlpatterns = [
path('about/', views.AboutView.as_view(),name='about'),
use this pattern same as your main url
path('about/', views.AboutView, name='about'),
It's not good to follow 2 ways of creating urls, Since django==2.0 they have introduced very nice and easy way to declare urls.
In the old way...
from django.conf.urls import url
urlpatterns = [
url(r'^about/$', AboutView.as_view(), name="about")
]
But In the new way it's lot more cleaner...
from django.urls import path
urlpatterns = [
path('about/', view.AboutView.as_view())
]
But if you want to stick with the regular expressions, Use re_path() instead of path().
urlpatterns = [
re_path(r'about/$', view.AboutView.as_view())
]
In my it's better stay with one pattern, old or new but not both. It makes your code look more cleaner.

Why NoReverseMatch at occurred on django 2.0

Error says :
Reverse for 'list' not found. 'list' is not a valid view function or pattern name.
My code are on below.
html template where error is in:
{% block more_posts %}<button type="button" href="{% url 'website:list' %}">more posts</button>{% endblock %}
my_project/urls.py:
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('website.urls')),
]
website/urls.py:
app_name = 'website'
urlpatterns = [
re_path(r'^about/$', TemplateView.as_view(template_name='website/about.html'), name='list'),
re_path(r'^$', views.main, name='main'),
]
Is there are something wrong with my url namespace settings?
add namespace
re_path(r'^', include('website.urls',namespcae="website")),

Django flaky url (randomly omits WSGIScriptAlias)

My Django site is producing URLs that intermittently omit my WSGIScriptAlias. If I simply print out {% url 'index' %} in my index.html (see my urls.py settings below) I randomly (around 50% of the time) get either:
MySiteAlias/MySite
which is correct, or
MySite/
which is incorrect.
myapp/urls.py:
from django.conf.urls import url,include
urlpatterns = [
url(r'^MySite/', include('mysite.urls')),
]
mysite/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
and views.index basically does return render(request, 'mysite/index.html). Any ideas on how to fix this?
I'd imagine you might have two urls with the same name, this is where namespaces will help. If you provide a namespace for your mysite.urls then there is no confusion on where you should go to
url(r'^MySite/', include('mysite.urls', namespace='mysite')),
{% url 'mysite:index' %}

One out of all urls is not matching

I'm using Django 1.8 and I can't figure out why one particular url isn't getting matched.
The url is /weapons. Django is adding a trailing slash to it which I believe is because of the APPEND_SLASH option being true by default. Even if I try to reach the url without the slash it will fail then try the slash.
This is the error I get:
top level urls.py
urlpatterns = [
url (r'^admin/', include (admin.site.urls)),
url (r'^', include ('core.urls', namespace = 'core')),
url (r'^', include ('equipment.urls', namespace = 'equipment')),
]
core urls.py
urlpatterns = patterns [
url (r'^$', views.index, name = 'index'),
]
equipment urls.py
urlpatterns = [
url (r'^equipment$', views.index, {'type':'index'}, name = 'index'),
url (r'^weapons$', views.index, {'type':'weapons'}, name = 'weapons'),
url (r'^armor$', views.index, {'type':'armor'}, name = 'armor'),
url (r'^accessories$', views.index, {'type':'accessories'}, name = 'accessories'),
]
I would do:
urlpatterns = [
url (r'^admin/', include (admin.site.urls)),
url (r'^home/', include ('core.urls', namespace = 'core')),
url (r'^equipment/', include ('equipment.urls', namespace = 'equipment')),
]
and
urlpatterns = [
url (r'^weapons/$', views.index, {'type':'weapons'}, name = 'weapons'),
]
note the [] instead of patterns in django 1.8
your url would look like:
http://localhost:1000/equipment/weapons/
which makes sense right?
Try to remove leading ^ from urls in core/urls.py and equipment/urls.py.
Turns out it was a caching issue. I tried it using Ctrl + F5 but it didn't work so I tried the page in Incognito mode which worked. So I used the Developer Tools to reload the page and now it works.