I don't have a "DATA" attribute in my view.py request


David

I am trying to pass data using jsfunction UpdateUserOrder

$(document).ready(function() {
    document.querySelectorAll('.update-cart').forEach(item =>{
        item.addEventListener('click', () => {
            var productId = item.dataset.product
            var action = item.dataset.action
            if(user === 'AnonymousUser'){
                console.log('not logged in')
            }else{
                UpdateUserOrder(productId, action)
            }
        })
    })
})

function UpdateUserOrder(productId, action){
    console.log('user logged in')

    var url = 'add_to_cart/'

    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type':'application/json',
            'X-CSRFToken': csrftoken,
        },
        body:JSON.stringify({'productId':productId,'action':action})
    })
    .then((response) =>{
        return response.json()
    })
    .then((data) =>{
        console.log('data', data)
    })
}
{% for product in products %}
    <div>
        <label id="title-label" for="title">Title</label>
        <p id="title">{{ product.title }}</p>
        <label id="author-label" for="author">Author</label>
        <p id="author">{{ product.author }}</p>
        <label for="user">Seller</label>
        <p id="user">{{ product.user }}</p>
        <p><img src="{{ product.image }}" alt="none"></p>
        <a id="link" href="{% url 'market_detail' product.id %}">Details</a>
        <button data-product="{{ product.id }}" data-action="add" class="update-cart">add</button>
    </div>
{% endfor %}

views.py:

def home(request):
    products = Product.objects.filter(sold=False)
    context = {'products': products}
    return render(request, 'market/home.html', context)


def add_to_cart(request):
    data = json.loads(request.DATA)
    productId = data['productId']
    action = data['action']
    print('action:', action, 'productId:', productId)
    return JsonResponse('item was added', safe=False)

urls.py:

urlpatterns = [
    path('', home, name='home'),
    path('detail/<int:pk>/', BookDetailView.as_view(), name='market_detail'),
    path('search/', book_search, name='book_search'),
    path('add_to_cart/', add_to_cart, name='add_to_cart'),
    ]

The error I get in the console:

What I'm saying in pycharm's terminal is:

AttributeError: 'WSGIRequest' object has no attribute 'data'

Murillo Citonio

You are sending data through the request body ( body:JSON.stringify({'productId':productId,'action':action})) , which means you have to access the data in views.py:

data = json.loads(request.body)

Also, you can always use built-in objects to inspect properties of objects .dir

Related


I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

How can I select elements that don't have an ID attribute?

stack I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wi

How can I select elements that don't have an ID attribute?

heap I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wit

How can I select elements that don't have an ID attribute?

stack I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wi

How can I select elements that don't have an ID attribute?

stack I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wi

How can I select elements that don't have an ID attribute?

stack I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wi

How can I select elements that don't have an ID attribute?

stack I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wi

How can I select elements that don't have an ID attribute?

stack I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wi

How can I select elements that don't have an ID attribute?

stack I have two textarea, one of them has idattributes and the other doesn't. like this: <textarea> </textarea> <textarea id = 'idname'> </textarea> Now I need to select the first text area, how can I do that? Josh Crozier You can use :not()pseudo-classes wi

I don't have .bash_profile on my mac

fumeng I'm trying to develop with Eclipse/Maven on a Mac and I don't have a .bash_profile when setting the environment variables. I do ls -a and it still doesn't exist. I see a .bash_history and .bash_sessions. Where should I set JAVA_HOME and PATH? Thanks! Br

I don't have Intel Virtualization Technology in my BIOS

murder I am trying to install Intel x86 Emulator Accelerator in Android Studio. I can't complete the installation because it says I have to enable Intel Virtualization Technology. I googled a bit and found that it can be enabled in the BIOS, but I can't find s

I don't have any value in my output file

Blair Shen My file contains "name" and 5 eye movement values (TFF, TFD, TVD, FB, FC). If the rows under the "Name" column are the same, I want to sum up the movement values for each eye. It seems the code is running without any errors, but my output file is st

I don't have VLC Media Player in my Ubuntu software

XBorge I can't find VLC Media Player in the new Ubuntu software. If I use the "apt://vlc" link from the official website, I get the message "Cannot find package 'vlc'". Is VLC still available on Ubuntu byte commander The vlcpackage is in a repository that univ

I don't have a system image in my Android SDK Manager

Ready I've searched for 1 whole day, but there is no clear answer to my problem. I want to install a system image for Android 6.0 (API 23), but the SDK Manager doesn't show me any options about this. Use the latest version of the standalone SDK Manager. Tools

I don't have a system image in my Android SDK Manager

Ready I've searched for 1 whole day, but there is no clear answer to my problem. I want to install a system image for Android 6.0 (API 23), but the SDK Manager doesn't show me any options about this. Use the latest version of the standalone SDK Manager. Tools