您的帮助对我将非常好。这是这些代码:
models.py:
from django.db import models
class TagModel(models.Model):
tag = models.CharField(max_length=50)
def __str__(self):
return self.tag
class MyModel(models.Model):
title = models.CharField(max_length=50)
tag = models.ManyToManyField(TagModel)
forms.py:
from django import forms
from .models import *
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = '__all__'
views.py:
from django.shortcuts import render, get_object_or_404, redirect
from .models import *
from .forms import *
def MyWriteView(request):
if request.method == "POST":
mywriteform = MyForm(request.POST)
if mywriteform.is_valid():
confirmform = mywriteform.save(commit=False)
confirmform.save()
return redirect('MyDetail', pk=confirmform.pk)
else:
mywriteform = MyForm()
return render(request, 'form.html', {'mywriteform': mywriteform})
form.html(第一次试用):
<form method="post">
{% csrf_token %}
{{ mywriteform }}
<button type="submit">Save</button>
</form>
form.html(第二次试用):
<form method="post">
{% csrf_token %}
{{ mywriteform.title }}
<select name="tags" required="" id="id_tags" multiple="">
{% for taglist in mywriteform.tags %}
<option value="{{taglist.id}}">{{taglist}}</option>
{% endfor %}
</select>
<button type="submit">Save</button>
</form>
我正在尝试在帖子上添加标签。我做了一个简单的manytomany标签博客,但是它不起作用。我通过单击“保存”按钮提交了帖子,标题已保存,但标签未保存。在管理员中,效果很好。
谢谢。
答案 0 :(得分:0)
像这样更新代码
var arr1 = {
AccessCX11410160916546: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "5.5.5.5",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
}
};
satelliteListByGroup.Default.push(arr1);
了解更多详情,Refer here