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
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.
What is the best way to get my module name as a function paramteter (entity) in request function?.
I tried the following but is not working at all (Page not found). Something is wrong in the url regex? :
url(r'^<entity>=accounts/(?P<pk>[0-9a-z\-]+)/activate_entity/$', activate_entity)
def activate_entity(request, entity, pk):
entity_model = apps.get_model(app_label='entities', model_name=entity)
entity_object = entity_model.objects.get(pk=pk)
Your regex pattern is wrong. try the following
url(r'^(?P<entity>[a-zA-Z_]+)/(?P<pk>[0-9a-z\-]+)/activate_entity/$', activate_entity)
and the url will be,
/{entity_name}/{pk}/activate_entity/
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')
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
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.