Pass dictionary data to Django view

白昼怎懂夜的黑 提交于 2021-01-07 06:43:50

问题


I'm confused on how to set up/call my url to pass a dictionary of data from my template to my view. I'm getting an error "NoReverseMatch at /categories/academy/" How can I pass data_dict, which is a dictionary, to my view?

template.html

<a href="{% url 'polls:request_access' data_dict %}" class="btn btn-green btn-sm"><i class="fa fa-plus"></i> Join Group</a>

urls.py

# the category_slug in this case is "academy", see the error I mentioned above
url(r'^categories/(?P<category_slug>[-\w]+)/request_access/$', 'request_access', name='request_access')

views.py

def request_access(request, data):
    print("DJANGO VIEW- THIS IS NOT PRINTING")
    mydata = request.GET.get('data_dict')  # will this work?
    # do other stuff
    return render(request, 'polls/categories/group_access_requested.html',
        {'data': request})

回答1:


Try creating a decorator to perform the encoding of the data_dict.

import urllib
from django import template
register = template.Library()

@register.filter
def get_encoded_dict(data_dict):
    return urllib.urlencode(data_dict)

Then inside your template, you can use it as:

<a href="{% url 'polls:request_access' my_category_slug %}?{{data_dict|get_encoded_dict}}" class="btn btn-green btn-sm"><i class="fa fa-plus"></i> Join Group</a>

This ought to solve your problem.




回答2:


To pass parameters to a url in a template, you will have to encode the parameters and then attach it at the end of the url.

You need to do something like:

<a href="{% url 'polls:request_access' my_category_slug %}?key1=value1&key2=value2.." class="btn btn-green btn-sm"><i class="fa fa-plus"></i> Join Group</a>

Another option is to use urllib.urlencode() from urllib library to encode the dictionary to url-encoded parameters. Just pass a dictionary to it and it will convert it to url-encoded string.

In [1]: import urllib

In [2]: data_dict = {'key1':'value1', 'key2':'value2', 'key3':'value3'}

In [3]: urllib.urlencode(data_dict)
Out[3]: 'key3=value3&key2=value2&key1=value1'

Then you can pass this url-encoded string to the context as my_url_encoded_string and place it at the end of the url.

In your views, then you can access the data_dict by its keys.

value1 = request.GET.get('key1') # value of 'key1'
value2 = request.GET.get('key2') # value of 'key2'



回答3:


I would recommend using Ajax for that. Here's an example to base your code off of:

html:

# Instead of passing info through here, I would set something up in your model to handle that
<a href="{{ object.get_some_url }} class="ajax_call" data="enter_value_here">Join Group</a>

models.py:

class Model(models.Model):
    field = models.ForeignKey(OtherModel)

    def get_some_url(self):
        return reverse('url_name',
                       kwargs={'url_argument': self.field.value})

ajax:

$('.ajax_call').click(function(e){
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "{% url 'some_url' %}",
        data: {
            "data_to_be_sent": $(this).attr("data"),
            csrfmiddlewaretoken: "{{ csrf_token }}",
        },
        dataType: "json",
        success: function(data) {
            if (data.worked) {
                // Do something here
            } else {
                // Do something else here
            }
        },
        error: function (rs, e) {
            alert('Sorry, there was an error.');
        }
    });
});

views.py:

from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_http_methods

from datetime import datetime


@require_http_methods(['POST'])
def textbook(request):
    data_received = request.POST.get('data_to_be_sent')
    # Do something with this data
    if something_was_done:
        # Do something?
        worked = True
    else:
        # Do something else?
        worked = False

    data = {
        'worked': worked
    }
    return JsonResponse(data)


来源:https://stackoverflow.com/questions/31889635/pass-dictionary-data-to-django-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!