django Vue.js在select option标签中返回空值

mmvthczy  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(184)

我在我的profie.html中有这样一行代码,它应该使用vue.js v-for和{{ val.division_name }}来显示除法运算的值。相反,这段代码返回的除法运算值是空白的,但我可以告诉你除法运算正在渲染的值,它只是空白值。

  1. <select name="division_id" class="form-control form-select" id="division-id" aria-label="Default select example" v-model="formData.division_id" @change="onDivisionChange" required>
  2. <option value="0">Select Division</option>
  3. <option v-for="val in divisionListVue" :value="val.id">{{ val.division_name }}</option>
  4. </select>

字符串
x1c 0d1x的数据
这是我的custom-vue.js

  1. const app = Vue.createApp({
  2. data() {
  3. return {
  4. divisionListVue: [], // Initialize divisionList with an empty array
  5. calendarListVue: [], // Initialize calendarList with an empty array
  6. formData: {
  7. division_id: 0, // Initialize division_id with 0
  8. division_name: '', // Initialize with an empty string
  9. calendar_id: null, // Initialize calendar_id with 0
  10. calendar_name: '', // Initialize with an empty string
  11. },
  12. };
  13. },
  14. methods: {
  15. showModal() {
  16. // Show the Bootstrap modal by selecting it with its ID and calling the 'modal' method
  17. // $('#myModal').modal('show');
  18. $("#modal1").modal('show'); // Show the modal on page load
  19. },
  20. // Function to fetch division data
  21. fetchDivisionData() {
  22. fetch('/users/api/divisions/') // Replace with the actual API endpoint
  23. .then(response => response.json())
  24. .then(data => {
  25. console.log(data);
  26. this.divisionListVue = data;
  27. console.log(this.divisionListVue);
  28. })
  29. .catch(error => {
  30. console.error('Error fetching division data:', error);
  31. });
  32. },
  33. // Function to fetch calendar data
  34. fetchCalendarData() {
  35. fetch('/users/api/calendars/') // Replace with the actual API endpoint
  36. .then(response => response.json())
  37. .then(data => {
  38. console.log(data);
  39. this.calendarListVue = data;
  40. console.log(this.calendarListVue);
  41. })
  42. .catch(error => {
  43. console.error('Error fetching calendar data:', error);
  44. });
  45. },
  46. onDivisionChange() {
  47. //console.log('Selected Division ID:', this.formData.division_id);
  48. //console.log(this.divisionListVue);
  49. const selectedDivision = this.divisionListVue.find(item => item.id === this.formData.division_id);
  50. this.formData.division_name = selectedDivision ? selectedDivision.division_name : '';
  51. },
  52. onCalendarChange() {
  53. //console.log('Selected Calendar ID:', this.formData.calendar_id);
  54. //console.log(this.calendarListVue);
  55. const selectedCalendar = this.calendarListVue.find(item => item.id === this.formData.calendar_id);
  56. this.formData.calendar_name = selectedCalendar ? selectedCalendar.calendar_name : '';
  57. },
  58. },
  59. mounted() {
  60. // Fetch division and calendar data when the component is mounted
  61. this.fetchDivisionData();
  62. this.fetchCalendarData();
  63. } // end of the mounted() function
  64. });
  65. app.mount('#app');

url.py

  1. urlpatterns = [
  2. path('register/', views.register, name='register'),
  3. path('login/', views.login_request, name='login'),
  4. path('profile/', views.profile, name='profile'),
  5. path('api/divisions/', views.get_divisions, name='get_divisions'),
  6. path('api/calendars/', views.get_calendars, name='get_calendars'),
  7. ]

views.py

  1. # function to get the list of divisions
  2. def get_divisions(request):
  3. divisions = Division.objects.all()
  4. data = [{'id': division.id, 'division_name': division.division_name} for division in divisions]
  5. return JsonResponse(data, safe=False)
  6. # function to get the list of calendars
  7. def get_calendars(request):
  8. calendars = Calendar.objects.all()
  9. data = [{'id': calendar.id, 'calendar_name': calendar.calendar_name} for calendar in calendars]
  10. return JsonResponse(data, safe=False)


这是console.log(data)返回的数据格式



这是console.log(this.divisionListVue);返回的数据格式



是什么原因导致vue显示空值?我已经有了vue使用这个脚本<script src="{% static 'js/vue.global.js' %}"></script>工作所需的静态文件。

i5desfxk

i5desfxk1#

我在Django项目中遇到的Vue.js无法按预期工作的问题与Vue.js的双花括号和Django的模板语法之间的冲突有关,Django的模板语法也使用双花括号作为模板变量。
当我在Django模板中使用{{ message }}时,它被解释为Django模板变量,而不是Vue.js表达式。为了避免这种冲突,我将Vue.js用于模板表达式的分隔符更改为{{ }}以外的东西。Vue.js提供了一个名为“自定义分隔符”的功能。它允许我为Vue.js模板表达式指定不同的分隔符。下面是我如何应用vue.js分隔符。

  1. const app = Vue.createApp({
  2. delimiters: ['{[', ']}'], // Change Vue.js delimiters to avoid conflicts with Django template tags
  3. });
  4. app.mount('#app');

字符串
然后分别应用除法和历法的运算符:

  1. <option v-for="val in divisionListVue" :value="val.id">{[ val.division_name ]}</option>
  2. <option v-for="val in calendarListVue" :value="val.id">{[ val.calendar_name ]}</option>

展开查看全部

相关问题