milan
milan

Reputation: 2417

NoReverseMatch at /add/

I keep getting error on my url tag. The syntax is right and i tried different solution that is asked on SO but no luck yet.

my urls.py

urlpatterns = [
    url(r'^$', LandingView.as_view(), name="landing_page"),
    url(r'^add/$', AddView.as_view(), name="add"),
    url(r'^rent/(?P<slug>\w+)/$', rent_detail, name="rent_detail"),
    url(r'^add/space/$', AddSpaceView.as_view(), name="addSpace"),
    url(r'^lang/$', Language.as_view(), name="lang"),
    url(r'^upload/image/(?P<pk>\d+)/$', ImageUpload, name="ImageUpload"),
    url(r'^filter/space/$', FilterSpace.as_view(), name="filterSpace"),
    url(r'^api/', include(v1_api.urls)),
]

views.py

def ImageUpload(self, request, *args, **kwargs):
    try:
        rental = Rental.objects.get(pk = kwargs.get('pk'))
    except Rental.DoesNotExist:
        error_dict = {'message': 'Rental spae not found'}
        return self.render(request,'rentals/add.html',error_dict)
    for file in request.FILES.getlist('image'):
        image = GalleryImage.objects.create(image=file,rental=rental)
    response_dict = {
        'message': 'File Uploaded Successfully'
    }
    context = {
        'rental':rental
    }
    return self.render(request,'rentals/add.html',context)

class AddView(TemplateView):
    template_name = 'rentals/add.html'

add.html

<div id="listing">
</div>
<script type="text/javascript">
  // want to passs url for uploading image in ajax url parameter
    var data = {
        urltag: "{% url 'ImageUpload' rental.id %}"
    }

    console.log('url is', data); 
    $(function() {
      app.showListingSpaceForm("listing",data);
    });

</script>

ajax for uploading image

 $.ajax({
        url:"/upload/image/", // upload form is in add.html which is developed using frontend library reactjs and i need to pass id here so that the image will be saved to its associated rent.
        data:image,
        contentType:false,
        processData:false,
        type:'POST',
        mimeType: "multipart/form-data",
        success: function(data) {
          console.log('success');
        }
       });
    }

Update: Ajax code where i want id in url for uploading image to its associated rent

Upvotes: 0

Views: 367

Answers (2)

Alasdair
Alasdair

Reputation: 308919

When you go to /add/, Django will run AddView because you have a url pattern.

url(r'^add/$', AddView.as_view(), name="add"),

That view will render rentals/add.html, because you have:

class AddView(TemplateView):
    template_name = 'rentals/add.html'

Rendering the template will fail, because you are using rental in the url tag, but you have not defined rental anywhere.

{% url 'ImageUpload' rental.id %}

Unless your AddView can set rental in the template context, then using the url tag like this will not work.

Upvotes: 3

Pierre Criulanscy
Pierre Criulanscy

Reputation: 8686

You need to explicitly pass down the kwarg argument since you defined it in your urls.py :

var data = {
    urltag: "{% url 'ImageUpload' pk=rental.id %}"
}

Upvotes: 2

Related Questions