Aplikasi pengalihanLink to this heading
Django comes with an optional redirects application. It lets you store simple
redirects in a database and handles the redirecting for you. It uses the HTTP
response status code 301 Moved Permanently by default.
PemasanganLink to this heading
Untuk memasang aplikasi pengalihan, ikuti langkah-langkah ini:
Pastikan bahwa kerangka kerja
django.contrib.sitesis installed.Tambah pengaturan
'django.contrib.redirects'to yourINSTALLED_APPS.Tambah
'django.contrib.redirects.middleware.RedirectFallbackMiddleware'ke pengaturanMIDDLEWAREanda.Jalankan perintah
manage.py migrate.
Bagaimana itu bekerjaLink to this heading
manage.py migrate creates a django_redirect table in your database. This
is a simple lookup table with site_id, old_path and new_path fields.
The RedirectFallbackMiddleware
does all of the work. Each time any Django application raises a 404
error, this middleware checks the redirects database for the requested
URL as a last resort. Specifically, it checks for a redirect with the
given old_path with a site ID that corresponds to the
SITE_ID setting.
If it finds a match, and
new_pathis not empty, it redirects tonew_pathusing a 301 ("Moved Permanently") redirect. You can subclassRedirectFallbackMiddlewareand setresponse_redirect_classtodjango.http.HttpResponseRedirectto use a302 Moved Temporarilyredirect instead.Jika itu menemukan kecocokan, dan
new_pathadalah kosong, itu mengirim sebuah 410 ("Gone") kepala HTTP dan tanggapan (kurang-isi) kosong.If it doesn't find a match, the request continues to be processed as usual.
The middleware only gets activated for 404s -- not for 500s or responses of any other status code.
Note that the order of MIDDLEWARE matters. Generally, you can put
RedirectFallbackMiddleware at the
end of the list, because it's a last resort.
Untuk lebih pada middleware, baca dokumentasi middleware.
Bagaimana menambah, merubah dan menghapus pengalihanLink to this heading
Melalui antarmuka adminLink to this heading
Jika anda telah mengaktifkan antarmuka admin Django otomatis, anda harus melihat bagian "Redirects" pada halaman indeks admin. Sunting pengalihan ketika anda menyunting obyek lain apapun di sistem.
Melalui API PythonLink to this heading
- class models.RedirectLink to this definition
Pengalihan adalah diwakili oleh Django model standar, yang tinggal di django/contrib/redirects/models.py. Anda dapat mengakses pengalihan obyek melalui Django database API.
MiddlewareLink to this heading
- class middleware.RedirectFallbackMiddlewareLink to this definition
Anda dapat merubah kelas-kelas
HttpResponsedigunakan oleh middleware dengan membuat sebuah subkelas dariRedirectFallbackMiddlewaredan terutamaresponse_gone_classdan/atauresponse_redirect_class.- response_gone_classLink to this definition
Kelas
HttpResponsedigunakan ketikaRedirecttidak ditemukan untuk jalur yang diminta atau mempunyai nilainew_pathkosong.Awalan pada
HttpResponseGone.
- response_redirect_classLink to this definition
Kelas
HttpResponseyang mengangani pengalihan.Awalan pada
HttpResponsePermanentRedirect.