如何调用view.py中的函数

时间:2017-09-07 20:33:34

标签: python django url-routing

这是当前的项目结构

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错误的原因是什么原因我在这里做错了

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您最有可能遇到正则表达式问题。

这种模式对我有用:(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')),
]

和(R6Scorextractor / R6scoreex / urls.py)

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

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

]