l have two url patterns in my Django
url(r'^myakun', 'portal.views.myakun', name='myakun'),
url(r'^myakun/about/$', 'portal.views.myakun_about', name='myakun_about'),
when l request to www.site.com/myakun it work well, but when I request to www.site.com/myakun/about/ it will force to redirect to www.site.com/myakun
this is my views
def myakun(request):
__return render_to_response("myakun.html", locals())
def myakun_about(request):
__return HttpResponse("sdasda")
Switch order of your urls like
url(r'^myakun/about/$', 'portal.views.myakun_about', name='myakun_about'),
url(r'^myakun', 'portal.views.myakun', name='myakun'),
In your case http://www.site.com/myakun/about/ matches with first url pattern ^myakun and hence shows your that page.
Or change the url pattern to add $
url(r'^myakun/$', 'portal.views.myakun', name='myakun'),
This url:
url(r'^myakun', 'portal.views.myakun', name='myakun'),
Should be:
url(r'^myakun/$', 'portal.views.myakun', name='myakun'),
Note the EOL matching $ added in. The problem is without that '^myakun' matches '/myakun/about' and that view is returned, it never even checks to see if '^myakun/about' matches, even though it does indeed match as well.
Related
I have site with django as backend and a url pattern like this in my urls.py file.
path('api/getnews/home/post/<str:titleInUrl>', getnews.getSpecificHomeNews , name='getspecificHomenews') ,
Now when I pass Normal Strings in my url like this, things go well:
api/getnews/home/post/Petrol,%20diesel%20prices%20are%20rising%20but%20why%20govt%20does%20not%20look%20worried
But when I pass anything with '#' symbol in it, python makes it a comment:
api/getnews/home/post/#MeToo:%20What%20Priya%20Ramani%20said%20after%20acquittal%20in%20MJ%20Akbar%20defamation%20case
I am not able to get my string after the # symbol.
I have two different urls:
1) /campaigns/, which lists available campaigns and 2)campaigns/add/ where there is a form to fill the details of a campaign to be created.
My django urls.py file looks like:
url(r'^campaigns/', views.campaigns, name='agency-campaigns'),
url(r'^campaigns/add/$', views.add_campaign, name='agency-add-campaign')
The problem is that whenever I browse the /campaigns/ url, it shows the correct template. But when I browse the /campaigns/add/ url, it still shows the /campaigns/ url template.
What can be wrong with this subdirectory?
Any ideas?
Thanks in advance!
That's because you don't have the end of string match ($) at the end of the regular expression:
url(r'^campaigns/$', views.campaigns, name='agency-campaigns'),
# HERE^
Or, you can also put the "agency-add-campaign" pattern before the "agency-campaigns":
url(r'^campaigns/add/$', views.add_campaign, name='agency-add-campaign'),
url(r'^campaigns/', views.campaigns, name='agency-campaigns')
Supposing that I have something like that in my urls.py:
url(r'^task/(?P<pk>[0-9]+)/', taskview, name='taskview')
How can django help me extract '10' (or {"pk": 10}) from reading any matching URL string like:
/task/10/
/task/10///
/task/10/dddd/dddd/dddwwwww/wwwwq/qqqqqqw/qwead/?adawd=awodawdoij
?
(FYI when I say "reading any URL string" I mean that I am not receiving a request on those urls, but rather that I have such a string in my code on which I have to perform the aforementioned keyword extraction)
Just figured it out:
from django.urls import resolve
resolve(url).kwargs
I try to code to django a redirect fonction. I provide the url, and i want to redirect to the provided URL.
in my urls.py:
urlpatterns = patterns('',
url(r'^redirect/(?P<name>.*)$', redirect),
# ...
)
when i test the function with a standard link (ex: google.com), it works perfectly.
when i test the function with a link that containt a "?" character, only the part before the "?" is taken into account.
Example :
"GET /redirect/http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=53713291 HTTP/1.1" 302 0
name = http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing
the ?id=53713291 is not taken into account....
i though that .* means all kind character, wrong?
Do you know what happens ? and how to take the entiere url what ever the character it contains?
thank you very much for your help.
You seems to don't understand how URL works. Everything after the ? is parsed as arguments for your current view. If you print data in your request.GET dict, you'll find something like:
{'id': 53713291}
The only way to fix that is to urlencode your URL in the argument and decode it before the redirection.
>>> import urllib
>>> urllib.quote_plus("http://www.polyvore.com/lords_liverpool_hey_jude_tee/thig?id=53713291")
'http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291'
# You should make your URL with this value, for example:
# /redirect/http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291
# And in your view, use unquote_plus before the redirection:
>>> urllib.unquote_plus('http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291')
'http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=5313291'
More information about Query String on Wikipedia.
You're passing in a regex, so you need to escape the special characters (ie, \?)
But if you're trying to pass in querystring parameters you'll want to handle that differently: https://stackoverflow.com/a/3711911
I have a Django based website. I would like to redirect URLs with the pattern servertest in them to the same URL except servertest should be replaced by server-test.
So for example the following URLs would be mapped be redirected as shown below:
http://acme.com/servertest/ => http://acme.com/server-test/
http://acme.com/servertest/www.example.com => http://acme.com/server-test/www.example.com
http://acme.com/servertest/www.example.com:8833 => http://acme.com/server-test/www.example.com:8833
I can get the first example working using the following line in urls.py:
('^servertest/$', 'redirect_to', {'url': '/server-test/'}),
Not sure how to do it for the others so only the servetest part of the URL is replaced.
Use the following (updated for Django 2.2):
re_path(r'^servertest/(?P<path>.*)$', 'redirect_to', {'url': '/server-test/%(path)s'}),
It takes zero or more characters after servertest/ and places them after /server-test/.
Alternatively, you can use new path function that covers simple cases url patterns without using regex (and it is preferred in new versions of Django):
path('servertest/<path:path>', 'redirect_to', {'url': '/server-test/%(path)s'}),
It's covered in the docs.
The given URL may contain dictionary-style string formatting, which will be interpolated against the parameters captured in the URL. Because keyword interpolation is always done (even if no arguments are passed in), any "%" characters in the URL must be written as "%%" so that Python will convert them to a single percent sign on output.
(Strong emphasis mine.)
And then their examples:
This example issues a permanent redirect (HTTP status code 301) from
/foo/<id>/ to /bar/<id>/:
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^foo/(?P<id>\d+)/$', redirect_to, {'url': '/bar/%(id)s/'}),
)
And so you see that it's just the nice straightforward form:
('^servertest/(?P<path>.*)$', 'redirect_to', {'url': '/server-test/%(path)s'}),
Try this expression :
('^servertest/', 'redirect_to', {'url': '/server-test/'}),
or this one:
('^servertest', 'redirect_to', {'url': '/server-test/'}),