0

I've seen Django's NoReverseError posted here lots of times, but none of the solutions seem to work in this specific instance.

My main goal is to create an image upload field in my object creation form which uploads multiple images to individual model objects. I'm following this tutorial because the end product fits perfectly.

The tutorial specifies that the data-url attribute should point to the route/view where the file form will be processed.

The form itself works just fine, and so does the url '/showroom/create'.

To make the Ajax form fit into my larger project, I had to change a few attributes:

  1. The word 'photos' to 'images' for consistency.
  2. I don't use the basic_upload view since it's on the same page as my create_car upload view. (which you'll see in the urlconf file, and works perfectly without this addon) Along with a few other tweaks.

Somewhere along the line, I broke the flow and I can't figure out what's wrong with it.

Here is my views.py for the Upload form.

class CarCreate(CreateView):
    model = Car
    fields = '__all__'
    success_url = reverse_lazy('showroom:car-detail')

    slug_field = 'id'
    slug_url_kwarg = 'car_create'

    def get(self, request):
        image_list = Car.objects.all()
        return render(self.request, 'showroom/car_form.html', {'images': image_list})

    def post(self, request):
        form = ImageForm(self.request.POST, self.request.FILES)
        if form.is_valid():
            image = form.save()
            data = {'is_valid': True, 'name': image.file.name, 'url': image.file.url}
        else:
            data = {'is_valid': False}
        return JsonResponse(data)

The relevant template markup:

  <button type="button" class="btn btn-primary js-upload-photos">
    <span class="glyphicon glyphicon-cloud-upload"></span>Upload images
  </button>
  <input id="fileupload" type="file" name="file" multiple
  style="display: none;"
  data-url="{% url 'showroom:images:image_upload' %}" 
  data-form-data='{"csrfmiddlewaretoken": "{{ csrf_token }}"}'>

App-specific urls.py

The relevant one is the car_create path.

app_name = 'showroom'

urlpatterns = [
    path('', views.index, name='index'),
    path('cars/', views.CarListView.as_view(), name="cars"),
    path('car/<uuid:car_detail>/', views.CarDetailView.as_view(), name='car-detail'),
    path('mycars/', views.SellerCarsListView.as_view(), name='my-cars'),
    path('create/', views.CarCreate.as_view(), name='car_create'),
    path('car/<uuid:car_update>/update/', views.CarUpdate.as_view(), name='car_update'),
    path('car/<slug:car_delete>/delete/', views.CarDelete.as_view(), name='car_delete'),
]

Here is the error raised:

Exception Type: NoReverseMatch at /showroom/create/
Exception Value: 'images' is not a registered namespace inside 'showroom'

After reading through other Stackoverflow answers, I've tried a few different solutions: 1. It's not a namespacing error since I've already attached the 'showroom:images'. 2. The problem resides within:

'data-url="{% url 'showroom:images:image_upload' %}"'

I know because if I change the data-url to '#', the page loads fine, but doesn't function.

Edit for more information

I read this guide to solving the answer, but it hasn't clearly solved my problem. If I understand correctly the other Stackoverflow answer addresses generating urlse from reverse functions.

My problem isn't with the url itself, showroom/create/ works perfectly fine. My problem comes with the image upload form I'm trying to create, and I've pointed to the specific line generating the error.

Is there any additional information needed?

Edit 2

I'll also add that my app is called 'showroom', and I defined the app_name variable in the app's urlconf page.

After reviewing the other pages, most of them have some explicitly defined context names which are then called in the template. Is there some way I can do that for the form?

I've been trying the answers from this other thread, like nesting the namespaces ('showroom:images:image_upload')and so far, no joy.

Rene
  • 370
  • 3
  • 14
  • 1
    Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Tanvir Islam Streame Oct 06 '19 at 09:37
  • show your`urls.py` file for both apps. – Abhyudai Oct 06 '19 at 10:56
  • Where is this `image_upload` URL defined? It *is* a namespace issue: as the error says, it is clear that there is no `images` namespace inside `showroom`. Show the `images` URLs and how they are included. – Daniel Roseman Oct 06 '19 at 11:58
  • So where should I route the images to instead? – Rene Oct 06 '19 at 12:10

0 Answers0