I have serious OOP design problem, and i don't know the way to solve it.
Here are two of my views:
And here is my "exercise form" which obviously used by "exercise view":
My field, used in form above:
And finally, simple template:
What i try to do:
I try to bargain two arguments which come from "create view" which not represent here in sake of space saving, and these two arguments are "language", and "dictionary".
I use them to get a record from db table, to get random text.
And here I need your help, because i can't dynamically create variable "exercise_text", because i need it in Class instance.
Meaning - it need to be predefined before I'll use my "exercise_field.
And also i can't put exercise_field inside of "__init__", because it will not be accessible in template.
In short:
I need to get random record from db, and i need to put this record in my field. How to do that? Dynamically.
-- Here are two of my views:
def exercise(request):
if request.method == 'GET':
request.session["exercise_start"] = time()
form = ExerciseForm(language = request.session["language"], dictionary = request.session["dictionary"])
elif request.method == 'POST':
form = ExerciseForm(request.POST, language = request.session["language"], dictionary = request.session["dictionary"])
if form.is_valid():
ExerciseTime = time() - request.session["exercise_start"]
ExerciseText = form.cleaned_data["exercise_field"]
if request.user.is_authenticated():
exercise = Exercise(player = request.user)
exercise.time = ExerciseTime
exercise.save()
request.session["exercise_text"] = ExerciseText
request.session["exercise_time"] = ExerciseTime
return redirect('game_result')
return render(request, "game/exercise.html", {'form': form})
def result(request):
exercise_result = {"exercise_text": request.session["exercise_text"], "exercise_time": request.session["exercise_time"]}
return render(request, "game/result.html", exercise_result)
And here is my "exercise form" which obviously used by "exercise view":
class ExerciseForm(forms.Form):
def __init__(self, *args, **kwargs):
self.language = kwargs.pop('language', None)
self.dictionary = kwargs.pop('dictionary', None)
self.exercise_text = self.getRandomText(lng=self.language, dct=self.dictionary)
self.SetExText()
super(ExerciseForm, self).__init__(*args, **kwargs)
def SetExText(self):
self.exercise_text = self.getRandomText(self.language, self.dictionary)
self.exercise_field.exercise_text = self.exercise_text
def getRandomText(self, lng, dct):
count = ExerciseText.objects.filter(dictionary__dictionary_name = dct).count()
if count:
rand_id = sample(range(1, count), 1)
return ExerciseText.objects.get(id__in=rand_id).text
else:
return "lol"
exercise_field = ExerciseField(exercise_text = exercise_text, widget = forms.TextInput(attrs = {'autocomplete': 'off', 'autofocus': 'autofocus'}))
My field, used in form above:
class ExerciseField(forms.CharField):
def __init__(self, exercise_text, *args, **kwargs):
self.exercise_text = exercise_text
super(ExerciseField, self).__init__(*args, **kwargs)
def validate(self, value):
if value != self.exercise_text:
raise ValidationError("Texts don't match")
And finally, simple template:
{% extends "base.html" %}
{% block content %}
<form action="game_result" method="post">
{% csrf_token %}
{{ form.exercise_text }}<br/>
{{ form.as_p }}
</form>
{% endblock content %}
What i try to do:
I try to bargain two arguments which come from "create view" which not represent here in sake of space saving, and these two arguments are "language", and "dictionary".
I use them to get a record from db table, to get random text.
And here I need your help, because i can't dynamically create variable "exercise_text", because i need it in Class instance.
Meaning - it need to be predefined before I'll use my "exercise_field.
And also i can't put exercise_field inside of "__init__", because it will not be accessible in template.
In short:
I need to get random record from db, and i need to put this record in my field. How to do that? Dynamically.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20b4a472-dae6-451c-8c66-85f53db67b6d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment