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.
Related
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
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.
How do I make web.py fetch a page when I click on a link?
I have this in my template:
<a href='add.html'>Home</a>
When I click on 'Home', I get 'not found'.
In my application, I have '/add' mapped to the 'Add' class which returns 'Boom!' using the template add.html.
urls = ('/', 'Index',
'/add','Add')
class Add(object):
def GET(self):
return render.add('Boom!')
I feel when I request for /add.html, the Add class will handle the request and return 'Boom!'
Why do I keep getting 'not found'?
The origin of a page URI ending in .html lilke /add.html is that in static hosting environments those were traditionally really single text files with html-content and the file ending for such a file is .html
But your system is dynamic and creates web pages on the fly. It does not necessarily need the pages ending in .html. Although you can mimic the traditional behaviour if you desire.
But in a modern and dynamic system it is often preferred that there is no ending on the single "pages" at all.
So you could, as Ignacio also suggests, just get rid of the .html and request the /add page, which is a valid and sufficient unique identifier for that resource.
If you like to keep the .html ending. you have to add it also in the route.
urls = (
'/', 'Index',
'/add.html','Add',
)
You can also have multiple routes pointing to the same resource, so that both /add and /add.html are valid and showing the same content, but content duplication has other drawbacks:
urls = (
'/', 'Index',
'/add','Add',
'/add.html','Add',
)
I recommend getting rid of the .html. This means you stick with the code from your question and create links to the page like this:
add something
Either change the route to be "/add.html" or change the link to be "add".