在python中的template中单独列出unicode数组



以下是我的看法.py

def UpdatePage(request):
    templatevar = {}
    user_name = request.session['login_user_email']
    update_form = Update_Page()
    if user_name:
        home_page_form = Home_Page
        templatevar['varobj']=models.ProfileDetail.objects.filter(email=user_name)
        if request.method=='POST':
        update_name=request.POST['name']
        update_age=request.POST['age']
        update_gender=request.POST.get('gender')
        update_interested_in=request.POST.getlist('interested_in')
        for i in update_interested_in:
            print i
        update_dob=request.POST.get('dob')
        update_country=request.POST['country']
        update_about_you=request.POST['about_you']
        update_profile_detail=models.ProfileDetail.objects.filter(email=user_name).update(name=update_name,age=update_age,gender=update_gender,
        interested_in=update_interested_in,dob=update_dob,country=update_country,about_you=update_about_you)
        if update_profile_detail:
            details_updated="Your Profile Details are Updated"
        return HttpResponseRedirect('homepage',{'details_updated':details_updated})
return render(request,'update.html',{'user_name':user_name,'update_form':update_form,'templatevar':templatevar})

以下是我的模板视图

   <html>
<head>
    <title>Update Here</title>
</head>
<body style="text-align:center">
<h3 style="text-align:right"><div><a href="homepage">Home</a></div><div><a href="logout">Log Out</a></div></h3>
{{user_name}}
    <h3>Update Here</h3>
    <!--{{update_form.as_p}}-->
    {% for all in templatevar.varobj %}
        <p>Age:{{all.age}}<br></p>
        <p>Gender:{{all.gender}}<br></p>
        <p>Interested In:{{all.interested_in}}<br></p>
        {%for i in all.interested_in%}
        {{i}}
        {%endfor%}
        <p>DOB:{{all.dob}}<br></p>
        <p>Country:{{all.country}}<br></p>
        <p>About You:{{all.about_you}}<br></p>
    {% endfor %}
    <form action="" method="post">
{% csrf_token %}
    {% for all in templatevar.varobj %}
<p><label for="id_name">People Call You As:</label> <input id="id_name" name="name" type="text" value="{{all.name}}" /></p>
<p><label for="id_email">Your Email:</label> <input id="id_email" name="email" type="email" value="{{user_name}}" readonly /></p>
<p><label for="id_age">Age:</label> <input id="id_age" name="age" type="number"value="{{all.age}}" /></p>
<p><label for="id_gender_0">Gender:</label> <ul id="id_gender">
<li><label for="id_gender_0"><input id="id_gender_0" name="gender" type="radio" value="Male" {% if all.gender == "Male" %}checked="checked"{% endif %} /> Male</label></li>
<li><label for="id_gender_1"><input id="id_gender_1" name="gender" type="radio" value="Female" {% if all.gender == "Female" %}checked="checked"{% endif %} /> Female</label></li>

<p><label for="id_interested_in_0">Interested In:</label>
<ul id="id_interested_in">
<li><label for="id_interested_in_0">
<input id="id_interested_in_0" name="interested_in" type="checkbox"     value="Sports" {% if all.interested_in == "Sports" %}checked="checked"{% endif %}/> Sports</label></li>
<li><label for="id_interested_in_1"><input id="id_interested_in_1" name="interested_in" type="checkbox" value="Music" {% if all.interested_in == "Music" %}checked="checked"{% endif %} /> Music</label></li>
<li><label for="id_interested_in_2"><input id="id_interested_in_2" name="interested_in" type="checkbox" value="Gardening" {% if all.interested == "Gardening" %}checked="checked"{% endif %}/> Gardening</label></li>

<p><label for="id_dob">Date Of Birth:</label> <input id="id_dob" name="dob" type="text" value="{{all.dob}}" placeholder = "YYYY-MM-DD" /></p>
<p><label for="id_country">Your From:</label> <select id="id_country" maxlength="3" name="country">
<option value="India">India</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
</select></p>
<p><label for="id_about_you">About You:</label> <textarea cols="40" id="id_about_you" name="about_you" rows="10" value="">{{all.about_you}}
</textarea></p>
{% endfor %}
<input type="submit" value="Update" />
</form>
</body>
</html>

以下是我的模型视图:

class ProfileDetail(models.Model):
name = models.CharField(max_length=100,null=True)
email = models.EmailField(max_length=50,unique=True,null=True)
age = models.IntegerField(max_length=3,null=True)
gender = models.CharField(max_length=10,null=True)
interested_in = models.CharField(max_length=10,null=True)
dob = models.DateField(null=True)
country = models.CharField(max_length=25,null=True)
about_you = models.CharField(max_length=200,null=True)
def __str__(self):
    return self.interested_in

我需要将复选框值单独显示为体育音乐园艺而不是以下内容感兴趣的领域:[体育,音乐,园艺]如何单独显示并检查值

您的错误是试图将列表存储到char字段中,这就是您看到列表的repr的原因。

首先,在您看来,更改行:

update_interested_in=request.POST.getlist('interested_in')

update_interested_in = ','.join(request.POST.getlist('interested_in'))

然后在model.py中添加一个拆分字符串的方法:

class ProfileDetail(models.Model):
    ...
    def interested_in_list(self):
        return self.interested_in.split(',')

然后,您可以使用join模板过滤器:

{{ all.interested_in_list|join:"<br/>"|safe }}

如果你必须自己构建复选框,你可以使用类似的东西:

{% for interest in all.interested_in_list %}
<input name="interested_in" type="checkbox" value="{{ interest }}"> {{ interest }}
{% endfor %}

相关内容

  • 没有找到相关文章

最新更新