django-http-proxy prepending slash - django

I have this in my urls.py
from httpproxy.views import HttpProxy
urlpatterns += patterns('',
url(r'^proxy/(?P<url>.*)$', HttpProxy.as_view(base_url=settings.PROXY_URL))
)
And my settings.py
...
PROXY_URL = 'http://external.com'
...
My problem is when accessing the URL http://localhost:8000/proxy/, I can see from the log of http://external.com it is returning 404 because the url has an extra slash prepended so for example:
http://localhost:8000/proxy/test/ will log "GET //test/ HTTP/1.1" 404 15447
I have been digging but couldn't find the bone! If all the masters would be kind enough to lend a bone for this hunger?
Cheers!

Since no one answered (I even got a badge because no one answered, how cool is that?), I will post my solution, which was solved 2 days after the question was asked.
1 - Because of this issue pointed out by a friend, I have steered away from using django-http-proxy.
2 - So I resorted to a better library which proxies all HTTP Methods, unlike django-http-proxy that can only proxy GET. Meet django-revproxy.
3 - Which introduces another problem — The Cookie Conflict. This happens because I have two django instances. The solution is to explicitly declare the cookie path in one of your django app so it won't be using the same path. Just add in settings.py these two lines:
SESSION_COOKIE_NAME = "yourApp_session_id"
CSRF_COOKIE_NAME = "yourApp_csrftoken"
4 - That's it. I hope this solution will help those on the lookout.

Related

Django missed slash in url

I created a url like 'api/personal/'. Everything went right when I did local test using './manage.py runserver'. But when I used factoryboy to create a client and try to get the detail by 'self.user_client.get('api/personal/')', the response showed 404 NOTFOUND because the url had changed to apipersonal/. Does anyone know why did it happen?
Use named urls for avoiding this kind of confusions. Define the url like this:
path('api/personal/', your_view, name='api_personal') # added keyword argument name
and use it in the tests with reverse like this:
self.client.get(reverse('api_personal'))

What is the ideal format/structure of urlconfs in django 2

I creating an application and one of its urlconf is as follows
urlpatterns = [
path('', DashboardView.as_view(),name='dashboard:index'),
]
I am coming from PHP background (say Laravel) where we name our routes like below
dashboard:index - for get
dashboard:store - for post
dashboard:update - for patch etc...
So I named my route as above, but while performing the system check, the following warning comes up.
System check identified some issues:
WARNINGS: ?: (urls.W003) Your URL pattern '' [name='dashboard:index']
has a name including a ':'. Remove the colon, to avoid ambiguous
namespace references.
System check identified 1 issue (0 silenced).
So my question is what is the ideal naming format of URLs in Django in general.
dashboard_index ?
dashboard.index ?
I guess the best place to find some kind of convention is in the django admin app, where I found this:
urlpatterns = [
url(r'^$',
views.BaseAdminDocsView.as_view(template_name='admin_doc/index.html'),
name='django-admindocs-docroot'),
url(r'^bookmarklets/$',
views.BookmarkletsView.as_view(),
name='django-admindocs-bookmarklets'),
# and so on...
]
So, a string representing the url with dashes between words. I think is also important the name to be very explicit, not acronyms or shortened names.
EDIT:
Example of general url/path naming from the docs (2.1):
path('archive/', views.archive, name='news-archive')
Also it's a good idea to have in mind python code style.

set a parameter with default value to route url in django

Hy all!
I'm new to python / django and I came across a problem that I can not solve. I have a route configured for the site's home (1) and a route configured for categories (2):
1) url(r'^$', IndexView().home, name='home')
2) url(r'^categoria/(?P<path>.*)/$', IndexView().by_category, name='by_category')
I need to set my home url to open a category by default, something like www.mysite.com/c=defaul_category
I tried in some ways, including: url (r '^ / (? P \ w +) / $', IndexView (). Home, name = 'home'). But I know it's incorrect.
So... I have no idea how to do this. Could someone help me?
Thank you
You should tell django that path in by_category url may be omitted. You have at least two options here:
1 - create one more url without path but with passed path variable as 3-rd argument in url:
url(r'^/(?P<c=vinhos>\w+)/$', IndexView().home, name='home')
url(r'^categoria/(?P<path>.*)/$', IndexView().by_category, name='by_category')
url(r'^categoria/$', IndexView().by_category,
{'path': 'default_path'}, name='default_category')
2 - change regex pattern to make it possible to omit path parameter. Here | (or sign) added in the end of path group:
url(r'^categoria/(?P<path>.*|)/$', IndexView().by_category, name='by_category')
More about omitting url parameters Django optional url parameters

Django URL reversing NoReverseMatch issue

I'm trying to use reverse to redirect a user to a login page from a third party App I'm using.
The URL config has:
urlpatterns = [
# authentication / association
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
name='begin'),
How can I accomplish this? I've tried
return redirect(reverse('social:login'), args=('facebook',))
and
return redirect(reverse('social:login'), kwargs={'backend':fb})
(to get to /login/facebook) but I'm getting a NoReverseMatch
The Django URL system and RegExes are confusing me a bit =(
EDIT: All right, it looks like I was making a mess with these URLs.
A simple solution that works (thank you #Alasdair in the comments):
return redirect('social:begin', backend='facebook')

Django: Permanent redirect of URL with regex parameters

I've been looking all over and can't find what I'm looking for.
I've found a way to redirect urls without parameters and keywords - but how to do it with parameters?
I want to redirect this:
(r'^andelsboligforeninger/(?P<page>[\d]*?)/$', 'cooperatives'),
to this:
(r'^liste-over-andelsboligforeninger/(?P<page>[\d]*?)/$', 'cooperatives'),
It should be a permanent redirect. This will be good for the SEO, and I get so many debug mails because of googlebot.
It seems I've found my answer in the django docs - I didn't look hard enought after all!
https://docs.djangoproject.com/en/1.1/ref/generic-views/
urlpatterns = patterns('django.views.generic.simple',
('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
)
First of all you need to do some changes in the url. Use url function and then give a name to the url. You have some issues in your url, for example you have used ?P but did'nt give a name to the capturing group. Second [\d]*? there is no need for ? because * means there can be a digit or not at all. So after considering all the above mentioned bugs and techniques in the end your url should look like this:
url(r'^liste-over-andelsboligforeninger/(?P<cooperative_id>\d*)/$', 'cooperatives', name="cooperatives")
Then in the view you can use reverse url resolution as:
redirect(reverse('cooperatives', kwargs={'cooperative_id': some_id}))