Loading an image from Vue.js and Django

I'm currently working on a project using Vue, and I'm fetching an image through a Django REST API.

The code in table.vue file:

<tbody>
  <tr v-for= "user in users" :key="user.id_users">
    <th>{{user.id_users}}</th>
    <td>{{user.name}}</td>
    <td>{{user.username}}</td>
    <td>{{user.profile}}</td>
    <td>   
      <img 
        src='{{user.photo}}' 
        class="img-circle elevation-2" 
        alt="User Image" 
        width="60"
      >
    </td>
    <td>{{user.status}}</td>
    <td>{{user.last_login}}</td>
    <td class="text-center">
      <button 
        @click="modify=true; openModal(category)" 
        type="button" 
        class="edit btn btn-primary">
        <i class = "fa fa-pencil-alt"></i>
      </button>
    </td>
    <td class="text-center">
      <button
        @click="delete(category.id_category, category.category)"
        type="button"
        class="remove btn btn-danger"
        data-toggle="modal"
        data-target="#delete-modal"
      >
        <i class="fas fa-dumpster-fire"></i>
      </button>
    </td>
  </tr>
</tbody>

This is the method used for fetching data:

async fetchUsers() {
  const response = await axios.get('https://inventory-control-system.herokuapp.com/users/');
  this.users = response.data;
},

The image link displayed as a result:

https://i.sstatic.net/TwKok.png

The actual URL that shows up when inspecting the photo element in the browser terminal:

https://i.sstatic.net/55g6Y.png

How can I correctly display the fetched images? Because when I use {{user.photo}} in the source attribute of the img tag, it doesn't work as expected.

Answer №1

To link the source, you can try:

:source='user.picture'

If that does not work, another approach is to create a method:

methods() {
  fetchImage(imagePath) {
    return require(imagePath);
  }
}

Then, in the template, call this method:

<img :src="fetchImage(user.picture)" alt="">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best starting dataset to use from a large pool of data points when creating a stock line chart in High

I am working on creating a line stock highchart similar to the example shown here: https://www.highcharts.com/demo/stock/lazy-loading In the provided example, the initial load fetches data from https://demo-live-data.highcharts.com/aapl-historical.json, s ...

Automatically scrolling down a div as new content is added using XMLHTTPRequest.openConnection

https://jsfiddle.net/kv5gbamg/ - I created a jsfiddle to demonstrate the functionality of the system Essentially, I am seeking a way to automatically scroll the scrollbar to the bottom every time a new message is received. My system updates the div with ...

Error in Function Due to Undefined jQuery DataTable Parameters

In the jQuery datatable column, the below JavaScript code will display either a green checkmark button or a red X button: { data: "HasPayment", render: function (data, type, row, meta) { if (data) { return '<bu ...

Accessing an element by its ID with the help of a looping

Looking to retrieve a string from my database and insert it into my paragraphs: <p id="q_1"></p> <p id="q_2"></p> The following code works correctly: $.get("bewertung/get/1", function (data) { document.getElementById("q_1") ...

Can you explain the mechanics behind Google Voice Search? And is there an available API for it?

Is this the right place to ask how the voice-activated search feature on Google's homepage functions? I'm curious about whether it utilizes Flash, a plugin integrated into Google Chrome, or some other method to access the microphone. The idea of ...

Error encountered during the execution of the store method in Ajax CRUD operation

Greetings, I'm encountering an error in my AJAX code every time I try to execute the store function https://i.sstatic.net/SW86I.jpg Below is my controller: public function store_batch(Request $request) { $rules = array( 'batch_name& ...

A step-by-step guide on accessing grouped column data in Angular UI Grid

How do we access the data of all rows within a grouped column in Angular UI Grid? For instance, when looking at the Grouping Tutorial, how can we retrieve all the company names from the 'Company' column? I have successfully obtained the aggrega ...

Close dynamic overlay modal when client-side validation fails during submission

When a user clicks on the "Apply" button after opening a "confirm" modal, client-side validation checks all fields. However, in case of failure, the modal does not close and I am struggling to find a solution. I attempted to use an event handler on the su ...

Obtain an image from a different domain using an HTTP request in jQuery

I am attempting to retrieve an image from a different domain and have tried the following code snippet: var img = "http://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg?v=bc7c2f3904bf"; $http.jsonp(img).success(function() { con ...

React conditional statement within a map function embedded in a JSX element

Below is the function I have created to generate tabbed items: function ConstructTabs(props) { const tabs = props.tabs; const makeTabs = tabs.map((tab, index) => { <React.Fragment> <input className="input-tabs" id ...

Event that occurs when modifying a user's Firebase Authentication details

Monitoring User Actions with Firebase Authentication Within my application built using Angular, Node.js, and Firebase, I am seeking a method to track user events such as additions, modifications, and deletions. Is there a mechanism to recognize when a us ...

Reduce the combined character value of the title and excerpt by trimming the excess characters

I am seeking a solution to trim the total character count of a post title and excerpt combined. Currently, I can individually adjust the excerpt through theme settings and the post title using JavaScript. However, due to varying title lengths, the height ...

I am in search of a JavaScript or jQuery plugin for an HTML slider with a unique range functionality

I've been searching online but couldn't find a slider like the one shown in the attachment. Can anyone help? Is there a way to create this specific slider or is there an existing plugin or library for it? Please refer to the image below :https:/ ...

Automatically compute and convert currency formats using JavaScript

May I ask again? Hopefully you understand. I am looking to automatically calculate with a money format. Here is a demo: https://jsfiddle.net/xp4ky2gg/ This is my code... HTML code <table style="width:100%"> ...

When a user clicks on the div, it expands in size and then returns to

I developed a small quiz, where clicking on an answer moves the checkmark to the clicked div. const answers = document.getElementsByClassName('answer'); [].forEach.call(answers, function(answer) { $(answer).click(function() { if (!answer ...

What is the best way to upload this HTML file to create my own visual representation?

Feeling a bit unsure about which way to go for this problem, I don't have many options at the moment. Lately, I've been diving into Deep Learning and I'm interested in experimenting with Stanford's CS231N's Convolutional Neural Ne ...

In my current project, I am implementing a feature in Angular 6 to close a Bootstrap modal once the server successfully receives the necessary data

Here, I am working on creating the content for a CRUD component that saves data as needed. My goal is to make the modal disappear once the data is submitted. <div class="container"> <div class="table-wrapper"> <div class="table-ti ...

The Nuxt JS plugin only executes when a full reload occurs, not on every page change

In my Nuxt JS 2 project, I've set up a session.js file with the following content: export default function ({ app, context }, inject) { console.log('load session') } After doing a full page reload, the console.log statement executes. Howe ...

Steps for transferring a value to a different page after it has been selected from a listview

web page layout <!DOCTYPE html> <html lang="en"> <head> <title>Student Information</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="styleshe ...

React.js implementation of a checkout feature using in-game currency

I am currently working on a game that involves in-game currency, but I'm facing some confusion. It seems like I might be overcomplicating things. In my store, users can purchase items using the gold (max variable) they have. The shopping cart function ...