I am struggling with a basic redirect functionality.
I need to redirect all traffic not matching certain paths to another domain.
in my urls.py
re_path(r'^(?P<shortcode>[\w-]+)/$', core_views.myView)
and the corresponding function in views.py
def myView(request, shortcode=None):
url = 'www.newdomain.cz/' + str(shortcode)
return HttpResponsePermanentRedirect(url)
but what it does is - when called for example www.olddomain.com/sdfasd it redirects me to www.olddomain.com/sdfasd/www.newdomain.cz/sdfasd but I obviously need only www.newdomain.cz/sdfasd
what am I missing?
You need to use a fully qualified url.
def myView(request, shortcode=None):
url = 'http://www.newdomain.cz/' + str(shortcode)
See the doc here.
Related
the redirect url is
"liveinterviewList/2"
and, ofcourse, I declare that url in url.py
more over, when I type that url in browser manualy, it works well.
what's the matter?
more question.
at this case, I write the user_id on the url.
I think, it is not good way to make url pattern.
but I don't know how I deliver the user_id variable without url pattern.
please give me a hint.
What HariHaraSudhan left out was how to use parameters. For your case, you would want something like:
path(r'liveinterviewList/<int:userId>', ..., name='live-interview'),
And then when you are ready to reverse, use this:
reverse('app:live-interview', kwargs={ 'userId': userId })
where app is the name of the app in which your view lives. If your url lives in the main urls file , you don't need the app: prefix.
Django reverse function accepts the name of the path not the URL.
lets say i have url patterns like this
urlpatterns = [
path('/users/list', name="users-list")
]
In my view i can use like this
def my_view(request):
return redirect(reverse("users-list"));
You should add a name to your path url and use it to redirect.
As the django doc says :
urls :
urlpatterns = [
path('/name', name="some-view-name")
]
def my_view(request):
...
return redirect('some-view-name')
I have experienced using reverse within get_absolute_url method in the model, but I wish I have an idea about the difference between reverse and redirect, I have tried to search on google about it but there is almost nothing
I don't know what should I write also to convince stack overflow that I don't have any other description
Reverse and redirect have a different meaning. Here is a simple explanation:
reverse in Django is used to find the URL of a given resource. Let's say that you have a blog website and from the main page, you want to provide links to your blog posts. You can of course just hard-code /posts/123/ and just change the ID of your blog post in URL, but that makes it hard to change your URL for the post in the future. That's why Django comes with reverse function. All you need to do is to pass the name of your URL path (defined in your urlpatterns) and Django will find for you the correct URL. It is called reverse because it is a reverse process of determining which view should be called for a given URL (which process is called resolving).
Redirects are not specific to Django or any other web frameworks. Redirect means that for a given URL (or action), the user should be instructed to visit a specific URL. This can be done by sending a special redirect request and from there the browser will handle it for the user, so no user action is required in that process. You can use reverse in redirect process to determine the URL that the user should be redirected to.
GwynBleidD has given you the answer, but there is a reason why you might be getting confused. The Django redirect shortcut accepts arguments in several different forms. One of them is a URLpattern mane, with arguments, that is then passed to reverse to generate the actual URL to redirect to. But that's just a shortcut, to enable a common pattern.
here's an example
app/views
#imports
def indexView(request):
....
return render(request, 'index.html', context)
def loginView(request):
....
return redirect('index')
def articleDetailView(request, id):
....
return redirect(reverse('article-comments', kwargs={'id':id})
def articleCommentsView(request, id):
....
return render(request, 'comment_list.html', context)
proj/urls
#imports
urlpatterns = [
....,
path('', include(app.urls))
]
app/urls
#imports
urlpatterns = [
....,
path('index/', index, name='index'),
path('login/', loginView, name='login'),
path('article/<int:id>/detail', articleDetailView, name='article-detail'),
path('article/<int:id>/comments/',articleCommentsView, name='article-comments')
....,
]
For loginView redirect will return url as-is i.e. 'index' which will be appended to base(project) urlpatterns. Here redirect(reverse('index')) will also work since kwargs is None by default for reverse function and 'index' view doesn't require any kwarg. It returns '/index/' which is passed to redirect(which again will be appended to base urls).
One thing to note is that reverse is used to make complete url - needed for redirect - that is shown in 'articleDetailview'.
The most basic difference between the two is :
Redirect Method will redirect you to a specific route in General.
Reverse Method will return the complete URL to that route as a String.
I'm working on my first django application, and I can't find anything in the docs that explains this.
I have a view called submit_proposal. If successful, it stores the relevant objects to the database then sends a user to a listing of all their open proposals. It displays correctly, but the listing page URL isn't shown by the browser.
Here's the code:
context = RequestContext(request,
dict(user_name=gc_user.get_full_name,
game_list=game_list,
POSTData=request.POST,
)
)
template = loader.get_template('user_proposals.html')
return HttpResponse(template.render(context))
This is called from submit_proposal, which passes the returned HttpResponse object back in turn:
result = user_proposals(request)
return result
I've looked at result in the debugger, and as far as I can tell it doesn't include a URL generated from the template name. Should I be doing something else to create the response? Or do I need to use something other than HttpResponse?
Thanks for your help!
Beverly
The most common way to do this in Django is to have two urls, for instance:
urlpatterns = patterns('',
url(r'^proposal/$', 'app.views.submit_proposal', name='submit_proposal'),
url(r'^proposal/all/$', 'app.views.list_proposals', name='list_proposals'),
Then the submit_proposal function should validate, store and redirect the user to list_proposals if successful
from django.shortcuts import redirect
def submit_proposal(request):
...
return redirect('list_proposals')
The argument to redirect is the name of the url for listing proposals.
well, if my request path is 127.0.0.1:8000/admin/user/edit/10, then I want to check whether the current user A has the permission to access the /admin/user/edit, when I use url = request.path, I get the url as /admin/user/edit/10.
But I want to get url = '/admin/user/edit' instead url = '/admin/user/edit/10'.So how can I get the correct url??much appreciate!!
Why don't you use the permission_required decorator above your view?
#permission_required('app.edit_right') # Fill in your permission
def edit_user(request):
#do stuff here
If the user (or the groups he's in) doesn't have this permission, then he will be redirected. Look for more info on the Django docs
I'm in c.html (http://localhost:8000/practice/c) and I click a link that changes my url to
http://localhost:8000/practice/c/?q=1
this is urls.py
url(r'^$', prac_c),
url(r'^q=\d',fetch_c),
and this is views.py
def prac_c(request):
return render_to_response('subject/c.html', {"problem_list":problem_list})
def fetch_c(request):
qno = request.GET.get('q')
if qno:
return render_to_response('subject/que.html', {'qno':qno}, context_instance=RequestContext(request))
but I'm not getting directed to que.html. Something wrong with the urls.py?
What the URLconf searches against
The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name.
source