![](/img/trans.png)
[英]Using the URLconf defined in name, Django tried these URL patterns, in this order
[英]Using the URLconf defined in Django tried these URL patterns in this order:
我有一个 django 应用程序。 以及一些上传功能。
上传 function 的页面正在正确加载。
但是在我提交之后。 我收到此错误:
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/main/
Using the URLconf defined in schoolfruitnvwa.urls, Django tried these URL patterns, in this order:
admin/
The current path, main/, didn’t match any of these.
所以这是 views.py
from .forms import ProfileForm
from .models import UploadFile
# Create your views here.
""" def store_file(file):
with open("temp/hello.png", "wb+") as dest:
for chunk in file.chunks():
dest.write(chunk) """
class CreateProfileView(View):
def get(self, request):
form = ProfileForm()
return render(request, "main/create_profile.html", {
"form": form
})
def post(self, request):
submitted_form = ProfileForm(request.POST, request.FILES)
if submitted_form.is_valid():
uploadfile = UploadFile(image=request.FILES["upload_file"])
uploadfile.save()
return HttpResponseRedirect("/")
return render(request, "main/create_profile.html", {
"form": submitted_form
})
网址.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.CreateProfileView.as_view())
]
forms.py:
class ProfileForm(forms.Form):
upload_file = forms.FileField()
所以我的问题是:我必须改变什么?
我这样尝试:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('main/', include('main.urls', namespace='main')),
]
但是后来我无法运行该应用程序:
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
在 urls.py 中:
from django.urls import path
from . import views
app_name='main'
urlpatterns = [
path("", views.CreateProfileView.as_view())
]
主要网址.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('main/', include('main.urls', namespace="app_main")),
]
设置.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
]
所以结构看起来像这样:
manage.py 位于文件夹中:schoolfruitnvwa。 在 schoolfruitnvwa 文件夹中,我有主要的应用程序。
您必须检查项目的 urls.py(此文件位于您的 settings.py 附近)中定义的 url 并添加。 小心 Django 得到 urls.py 文件 我不能在开始时有点打扰。
在 urls.py(靠近 settings.py)中::
path('main/', include('youappname.urls', namespace="app_namespace")),
在 urls.py(您的应用程序)中:
app_name = 'yourappname'
urlpatterns = [
path(...your api function call...),
]
哦,在 html 文件中有站着:/main/
<form action="/" method="post" enctype="multipart/form-data">
{% csrf_token %} {{ form }}
<button type="submit">Upload!</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.