English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
웹 애플리케이션에서 페이지 리디렉션은 여러 이유로 필요합니다. 특정 동작이 발생하거나 오류가 발생할 때 사용자를 다른 페이지로 리디렉션하고 싶을 수 있습니다. 예를 들어, 사용자가 웹사이트에 로그인하면 자주 그의 홈페이지나 개인한 보드로 리디렉션됩니다. Django에서는 'redirect' 메서드를 사용하여 리디렉션을 구현합니다.
redirect 메서드는 매개변수로 URL을 리디렉션할 URL의 문자열이 필요합니다.
myapp/views는 지금까지 다음과 같습니다 −
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: ko.oldtoolbag.com # Date : 2020-08-08 def hello(request): today = datetime.datetime.now().date() daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] return render(request, "hello.html", {"today" : today, "days_of_week" : daysOfWeek}) def viewArticle(request, articleId): """ A view that displays an article based on its ID """ text = "Displaying article Number: %s" %articleId return HttpResponse(text) def viewArticles(request, year, month): text = "Displaying articles of: %s"/%s"%(year, month) return HttpResponse(text)
hello을 djangoproject.com으로 리디렉션하도록 수정하고, viewArticle를 내부의 ''로 리디렉션하도록 수정합니다./myapp/articles' myapp/view.py를 다음과 같이 수정합니다:
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: ko.oldtoolbag.com # Date : 2020-08-08 from django.shortcuts import render, redirect from django.http import HttpResponse import datetime # Create your views here. def hello(request): today = datetime.datetime.now().date() daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] return redirect("https://www.djangoproject.com) def viewArticle(request, articleId): """ A view that displays an article based on its ID """ text = "Displaying article Number: %s" %articleId return redirect(viewArticles, year = "2045", month = "02") def viewArticles(request, year, month): text = "Displaying articles of: %s"/%s"%(year, month) return HttpResponse(text)
위의 예제에서는 Django에서 redirect를 import하고, Django 공식 웹사이트로 간단히 리디렉션하려면, 'redirect' 메서드로 전체 URL을 문자열로 사용합니다. 두 번째 예제( viewArticle 뷰에서 'redirect' 메서드)에서는 뷰 이름과 그 매개변수를 매개변수로 사용합니다.
접근합니다./myapp/hello을 입력하면 아래와 같은 화면이 표시됩니다.-
접근합니다. /myapp/article/42아래와 같은 화면을 표시합니다.-
temporary 또는 permanent인지 지정할 수 있습니다. 사용자에게는 차이가 없지만, 이들은 세세한 사항이며, 검색 엔진 웹사이트 순위를 고려합니다.
우리는 url.py에서 URL 매핑 시 'name' 매개변수를 정의합니다.
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: ko.oldtoolbag.com # Date : 2020-08-08 url(r'^articles}}/(?P\d{2}/(?P\d{4}/', 'viewArticles', name = 'articles'),
이 이름(이제의 문서)는 'redirect' 메서드의 인자로 사용될 수 있으며, viewArticle 리디렉션을 수정할 수 있습니다. -
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: ko.oldtoolbag.com # Date : 2020-08-08 def viewArticle(request, articleId): """ A view that displays an article based on its ID """ text = "Displaying article Number: %s" %articleId return redirect(viewArticles, year = "2045", month = "02")
변경하면 −
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: ko.oldtoolbag.com # Date : 2020-08-08 def viewArticle(request, articleId): """ A view that displays an article based on its ID """ text = "Displaying article Number: %s" %articleId return redirect(articles, year = "2045", month = "02")
비고 - URL 생성하는 함수도 있습니다. 이는 같은 방식으로 리디렉션되며; 'reverse' 메서드(django.core.urlresolvers.reverse)를 사용합니다. 이 함수는 HttpResponseRedirect 객체를 반환하지 않으며, 단순히 URL과 전달된 매개변수를 포함한 뷰 문자열을 컴파일합니다.