Python Django流程和导航

时间:2017-09-06 19:29:57

标签: python django

这是当前的项目结构

R6Scorextractor


        R6Scoreex
                 migrations
                 templates
                          R6Scoreex
                                   header.html
                                   home.html


                 __Init__.py
                 settings.py
                 urls.py 
                 views.py
                 models.py
                 apps.py
                 admin.py
                 tests.py
        R6Scorextractor
                 __Init__.py
                 settings.py
                 urls.py 
        manage.py

R6Scorextractor / R6scoreex / urls.py

from django.conf.urls import url
from . import views
from django.conf.urls import include

urlpatterns = [
    url(r'^$', views.index, name='index'),




]

R6Scorextractor / R6scoreex / views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

from django.shortcuts import render
from django.conf import settings
from django.core.files.storage import FileSystemStorage
# Create your views here.
from django.http import HttpResponse
import pdb;

def index(request):
    return render(request, 'R6scoreex/home.html')


def simple_upload(request):


    print "Entered simple_upload"

    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        return render(request, 'R6scoreex/home.html', {
            'uploaded_file_url': uploaded_file_url
        })
    return render(request, 'R6scoreex/home.html')

R6Scorextractor / R6Scorextractor / url.py

from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('R6scoreex.urls')),
]

我只是想知道如何在R6scoreex模块的views.py中调用simple_upload。如何为它编写URL,当我使用以下内容时,服务器给出了404错误

url(r'^/simple_upload/$', views.simple_upload, name='simple_upload'),

enter image description here

那么即使在添加上面的代码之后我得到404错误的原因是什么原因我在这里做错了

Click this link to download the copy of the site

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:2)

在R6Scorextractor中的urls.py上使用

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'R6scoreex/', include('R6scoreex.urls')),
]

这意味着您将所有R6scoreex.url映射到以&#34开头的路径; R6scoreex /"

这就是你可以从localhost访问的原因:8000 / R6scoreex / simple_upload /

如果您改为

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('R6scoreex.urls')),
]

您应该可以从中访问 本地主机:8000 / simple_upload /

我希望我明确表示,如果不随意提出任何问题

答案 1 :(得分:0)

这确实很奇怪。我花时间下载并设置你的django项目来检查可能出错的地方。这些是我采取的步骤:

  • 将文件解压缩到新文件夹。
  • 安装虚拟环境。 (我发现你使用的是python2)。

    virtualenv -p path/to/python2 env
    source env/bin/activate
    pip install django
    

我对您的项目所做的唯一更改是从settings.py中的ALLOWED_HOSTS = []到ALLOWED_HOSTS = [' *']。

然后我跑了它:

cd R6Scorextractor
python manage.py runserver 

它有效:

  • 我去了根网址和404:那是对的。
  • 然后到/ R6scoreex /和: enter image description here

  • / R6scoreex / simple_upload旁边 enter image description here enter image description here

我在想,你的问题是ALLOWED_HOSTS非常不可思议。完成所有这些后,我发现你使用的是错误的网址......

你不应该去127.0.0:8000/simple_upload/。您的设置指向127.0.0:8000/R6scoreex/simple_upload/

如果您需要127.0.0:8000/simple_upload/,则应将url(r'R6scoreex/', include('R6scoreex.urls'))更改为url(r'/', include('R6scoreex.urls'))

希望这可以解决问题。

答案 2 :(得分:-1)

R6Scorextractor / urls.py中的include应为空。

#1::
   WinTitle := "Untitled - Notepad"
   WinGet WinState, MinMax, %WinTitle%  ; retrieve minimized/maximized state
      if (WinState = -1)                ; minimized
         WinRestore, %WinTitle%
      else                              ; not minimized
         WinMinimize, %WinTitle%
Return