Using the URLconf defined in Django tried these URL patterns in this order:

Question:

I have a django app. And some upload functionality.

The page with the upload function is loading correcttly.

But after I do a submit. I get this errror:


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.

so this is the 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
        })

urls.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()

So my question is: what I have to change?

I try it like this:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('main/', include('main.urls', namespace='main')),
   
]


But then I cant run the app:

    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.

in urls.py:

from django.urls import path
from . import views

app_name='main'

urlpatterns = [
    path("", views.CreateProfileView.as_view())
]

main urls.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")),
   
]

settings.py:


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
    
]

and so the structure looks like this:

the manage.py is in the folder: schoolfruitnvwa. and in the folder schoolfruitnvwa I have the main application.

Asked By: mightycode Newton

||

Answers:

You must check the url defined in the urls.py (this files is located near your settings.py) of your project and add. Be careful Django got urls.py files I can’t be a bit disturbing at the start.

In urls.py (near settings.py): :

path('main/', include('youappname.urls', namespace="app_namespace")),

In urls.py (of your app):

app_name = 'yourappname'

urlpatterns = [
    path(...your api function call...),
]
Answered By: Herlock

ohhh, in the html file there was standing: /main/

  <form action="/" method="post" enctype="multipart/form-data">
      {% csrf_token %} {{ form }}
      <button type="submit">Upload!</button>
    </form>   
Answered By: mightycode Newton
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.