【问题标题】:Pass url with id to html page that is called by another url将带有 id 的 url 传递给另一个 url 调用的 html 页面
【发布时间】:2016-03-17 01:29:56
【问题描述】:

我有一个由 url /add 调用的模板,其中包含多个完全在 reactjs 上开发的注册页面。在由 url /add 调用的 add.html 页面中,也有一个图像上传表单(它也是使用 reactjs 开发的)并且有需要 url 发布的 ajax 代码,我需要不同的 url {% url 'uploadImage' pk %} 提供,以便我可以保存到其关联的实例。简而言之,表单在 add.html 中,表单需要 id 才能将数据保存到其关联的实例中。

这是我的代码

urls.py

url(r'^add/$', AddView.as_view(), name="add"), // presents add.html page which displays just multiple form
url(r'^upload/image/(?P<pk>\d+)/$', UploadImage.as_view(), name="uploadImage"), // used for uploading image that is on the add.html page

views.py

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

class UploadImage(View):
    model = Rental
    template_name = 'rentals/add.html'
    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)
    def post(self,request,*args,**kwargs):
        print(request)
        if request.FILES:
            rental = Rental.objects.get(pk=request.POST['rental'])
            print ('rental is', rental)
            for file in request.FILES.getlist('image'):
                print('file',file)
                image = Gallery.objects.create(image=file, rental=rental)
                print('image',image)
        return HttpResponseRedirect('/')

add.html

<div id="listing"> // this contains multiple form developed using reactjs code  
 </div>

{% include 'includes/script.html'%}
<script type="text/javascript">
    var data = {
        urltag: {% url 'uploadImage' %} // how can i pass url here if i pass with pk i get an error as this templated is called using /add but the form is also in this template which anyhow requires id 
    }
    console.log('url is', data); 
    $(function() {
      app.showListingSpaceForm("listing",data);
    });

</script>

ajax 代码

$.ajax({
      url:"/add/space/",
      data:sendData,
      type:'POST',
      success: function(data, textStatus, xhr ) {        
        $.ajax({
             url:"/upload/image/",
             data:image,
             contentType:false,
             processData:false,
             type:'POST',
             mimeType: "multipart/form-data",
             success: function(data) {
               console.log('success');
             }
        });
         window.location.href = "http://localhost:8000/";
     }
 });

【问题讨论】:

    标签: python ajax django reactjs django-views


    【解决方案1】:

    在您的视图中的响应头中返回一个“pk-user”

    response['pk-user'] = rental.pk
    return response
    

    现在您可以使用返回的用户 ID

     $.ajax({
          url:"/add/space/",
          data:sendData,
          type:'POST',
          success: function(data, textStatus, xhr ) {
            var pk = xhr.getResponseHeader('pk-user');
            console.log('pk is',pk);
            $.ajax({
                 url:"/upload/image/"+pk+"/",
                 data:image,
                 contentType:false,
                 processData:false,
                 type:'POST',
                 mimeType: "multipart/form-data",
                 success: function(data) {
                   console.log('success');
                 }
            });
             window.location.href = "http://localhost:8000/";
         }
     });
    

    【讨论】:

      【解决方案2】:

      应该是

      {% url 'uploadImage' 'pk_id' %}
      

      或者如果你有一个名为image的对象,请做

      {% url 'uploadImage' image.pk %}
      

      【讨论】:

      • 我收到一条错误消息,指出 NoReverseMatch 在 /add/ Reverse for 'uploadImage' 与参数 '('pk_id',)' 和关键字参数 '{}' 未找到。尝试了 2 种模式:['upload/image/(?P\\d+)\\.(?P[a-z0-9]+)/?$', 'upload/image /(?P\\d+)/$']
      • /add 负责 add.html,所以我不能将该 url 传递给 add.html
      • 但我的上传表单在 add.html 页面中。
      • 您确定 pk 是数字吗?您需要传递正确的参数格式,使其与模式匹配。
      • 是的,pk 是一个数字。我在 urlconf 中使用了正则表达式 /d。
      猜你喜欢
      • 1970-01-01
      • 2023-01-24
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2015-06-02
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多