我是django的初学者,我在这个项目中遇到了问题,我的目标是让每个用户给出1-10的分数。我的问题是如何制作" granted_by"得分" nota" to" granted_to"排除自己。我不知道如何在视图中编写它并在模板中使用它。在给我一个问题之前 - 请问一个愚蠢而简单的问题,请在我的代码中找到一个高峰。
models.py
from django.db import models
from django.conf import settings
VALUE = (
(1, "Nota 1"),
(2, "Nota 2"),
(3, "Nota 3"),
(4, "Nota 4"),
(5, "Nota 5"),
(6, "Nota 6"),
(7, "Nota 7"),
(8, "Nota 8"),
(9, "Nota 9"),
(10, "Nota 10"),
)
class Score(models.Model):
granted_by = models.ForeignKey(settings.AUTH_USER_MODEL, default=0)
granted_to = models.ForeignKey(settings.AUTH_USER_MODEL, default=0, related_name="granted_to")
nota = models.PositiveSmallIntegerField(default=0, choices=VALUE)
views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render
from pro1 import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .models import Punctaj
def Login(request):
next = request.GET.get('next', '/home/')
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(next) # render(request, "login/index.js")
else:
return HttpResponse('Inactive user.')
else:
return HttpResponseRedirect(settings.LOGIN_URL)
return render(request, 'login/login.html', {'redirect_to': next})
def Logout(request):
logout(request)
return HttpResponseRedirect(settings.LOGIN_URL)
@login_required
def Home(request):
user_list = User.objects.all()
context = {
"user_list": user_list,
}
return render(request, "login/home.html", context)
home.html的
<form class ="nota" method="POST" action="">{% csrf_token %}
<h3>Scores</h3>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th> User </th>
<th> Scor </th>
<th> Nota finala </th>
</tr>
</thead>
<tbody>
{% for fotbalist in user_list %}
<tr>
<td>{{ fotbalist.username }}</td>
<td><input type="number" name="scor" min="1" max="10"></td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<p><input type = "submit" value="submit"/></p>
</form>
答案 0 :(得分:0)
您可以在项目中使用类似的内容。
Models.py
class Score(models.Model):
granted_by = models.ForeignKey(settings.AUTH_USER_MODEL, default=0)
granted_to = models.ForeignKey(settings.AUTH_USER_MODEL, default=0, related_name="granted_to")
nota = models.PositiveSmallIntegerField(default=0, choices=VALUE)
View.py
class giveScoreView(FormView):
template_name = 'give_score.html'
form_class = giveScoreForm
def get_initial(self):
initial_dict = {}
initial_dict['user'] = self.request.user
return initial_dict
def form_valid(self, form):
form.instance.granted_by = self.request.user
form.save(commit=True)
def get_success_url(self):
url = "some return url"
return url
Forms.py
class giveScoreForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(giveScoreForm, self).__init__(*args, **kwargs)
loggedin_user = kwargs.get('initial').get('user')
qs = settings.AUTH_USER_MODEL.objects.filter().exclude(
username=loggedin_user.username)
self.fields['nota'].widget = forms.TextInput(attrs={
'id': 'nota',
'class': 'form-control',
'name': 'nota',
'placeholder': 'Score',
'required': True})
self.fields['granted_to'] = forms.ModelChoiceField(
queryset=qs,
to_field_name='email',
widget=forms.Select(attrs={'class': 'form-control',
'required': True})
)
class Meta:
model = Score
fields = ['granted_to', 'nota']
give_score.html
<form id="lead-form" role="form" method="post" action="{% url "giveScoreView" %}">
{% csrf_token %}
<div class="form-group">
<label>nota</label>
{{form.nota}}
</div>
<div class="form-group">
<label>granted_to</label>
{{form.granted_to}}
</div>
<button type="submit" class="btn btn-primary btn-lg">Add</button>
</form>