2017-02-05 58 views
0

我是Django的新成員,並且對URL縮短項目的工作比較單調。Django無法正確重定向

DEBUG-URLRedirectView::get(): obj.url: www.google.com 
[05/Feb/2017 19:02:27] "GET /gesw1m/ HTTP/1.1" 302 0 
Not Found: /gesw1m/www.google.com 
[05/Feb/2017 19:02:27] "GET /gesw1m/www.google.com HTTP/1.1" 404 2216 

enter image description here

這裏是:我想什麼時候點擊success.html模板創建的鏈接,它不會重定向,並添加路徑+ SHORT_URL +網址運行到問題的時刻項目樹:

-src_shrtnr 
    manage.py 
    -shrtnr 
     hosts.py 
     settings.py 
     urls.py 
     -hostconf 
     urls.py 
     views.py 
    -shortener 
     models.py 
     views.py 
     -templates 
     -shortener 
      already-exist.html 
      home.html 
      success.html 
    -analytics 
     ... 

這裏是success.html模板和圖像:

<div style='align-text: center; width: 800px; margin: 0 auto;'> 
    <h1>Created!</h1> 
    <p>{{ object.url }}</p> 
    <p>Count: {{ object.clickevent.count }}</p> 
    <p><a href='{{ object.get_short_url }}'>{{ object.get_short_url }}</a></p> 
    <a href='/'>+New</a> 
</div> 

enter image description here

這裏是縮短服務models.py文件:

class ShrtnrURL(models.Model): 
    url = models.CharField(max_length=220, validators=[validate_url, validate_dot_com]) 
    shortcode = models.CharField(max_length=SHORTCODE_MAX, unique=True, blank=True) 

    ... 

    def get_short_url(self): 
     url_path = reverse('scode', kwargs={'shortcode': self.shortcode}, host='www', scheme='http') 
     print('DEBUG-ShrtnrURL::get_short_url(), url_path: ', url_path) 
     return url_path 

這裏是縮短服務views.py相關觀點:

class URLRedirectView(View): 
     qs = ShrtnrURL.objects.filter(shortcode__iexact=shortcode) 
     if qs.count() != 1 and not qs.exists(): 
      raise Http404 
     obj = qs.first() 
     print('DEBUG-URLRedirectView::get(): obj.url: ',ClickEvent.objects.create_event(obj), obj.url) 
     return HttpResponseRedirect(obj.url) 

下面是hostconf urls.py:

from .views import wildcard_redirect 

urlpatterns = [ 
    url(r'^(?P<path>.*)', wildcard_redirect), 
] 

這裏是hostconf views.py:

DEFAULT_REDIRECT_URL = getattr(settings, 'DEFAULT_REDIRECT_URL', 'http://www.shrtnr.co') 

def wildcard_redirect(request, path=None): 
    new_url = DEFAULT_REDIRECT_URL 
    if path is not None: 
     new_url = DEFAULT_REDIRECT_URL + '/' + path 
    print('DEBUG-wildcard_redirect(), new_url: ', new_url) 
    return HttpResponseRedirect(new_url) 

這裏是hosts.py:

from shrtnr.hostsconf import urls as redirect_urls 
host_patterns = [ 
    host(r'www', settings.ROOT_URLCONF, name='www'), 
    host(r'(?!www).*', redirect_urls, name='wildcard'), 
] 

這裏是shrtnr urls.py:

from shortener.views import HomeView, URLRedirectView 
urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^$', HomeView.as_view()), 
    url(r'^(?P<shortcode>[\w-]+)/$', URLRedirectView.as_view(), name='scode'), 
] 

從settings.py選段:

ALLOWED_HOSTS = ['www.shrtnr.co', 'shrtnr.co', 'blog.shrtnr.co'] 
INSTALLED_APPS = [ 
    ... 
    # Third party 
    'django_hosts', 
    # Custom app 
    'analytics', 
    'shortener', 
] 
MIDDLEWARE = [ 
    'django_hosts.middleware.HostsRequestMiddleware', 
    ... 
    'django_hosts.middleware.HostsResponseMiddleware', 
] 
ROOT_URLCONF = 'shrtnr.urls' 
ROOT_HOSTCONF = 'shrtnr.hosts' 
DEFAULT_HOST = 'www' 
DEFAULT_REDIRECT_URL = 'http://www.shrtnr.co:8000' 
PARENT_HOST = 'shrtnr.co:8000' 

只是爲了指出我添加在/ etc/hosts中:

127.0.0.1 www.shrtnr.co 
127.0.0.1 shrtnr.co 
127.0.0.1 blog.shrtnr.co 

我仍然處於「調試」模式,尚未託管(將使用Heroku),但我在settings.py中添加了:8000端口。 我想我補充了所有必要的信息,請讓我知道,如果我忘記提及一些東西。它可能會解決,一旦我主持,但我不知道。

  • 的Python 3.5
  • Django的1.10
  • 安裝django_hosts 2.0通過蟒蛇

回答

1

問題成功是您的網址只是 「www.google.com」,沒有一個協議。當瀏覽器被告知重定向時,如果沒有協議,它只會將其視爲路徑 - 並且由於沒有初始斜線,它會將其視爲相對路徑並將其附加到當前位置。

您可能需要添加驗證以確保該字段包含完整的URL,包括協議,即「https://www.google.com」。或者,您的視圖可能會檢查該值並在必要時加上「http://」或「https://」。

+0

謝謝@ Daniel-Roseman,關於URL驗證的好主意! :) – camelBack