)My question is bed.
I read docs of course.
In another's Python frameworks I must write all classes to be used more 
than one time.
I make Button and it's.
If I need a button just call this class

class MyChoiceField(forms.Form):
    def __init__(self, foo_choices, *args, **kwargs):
        super(MyChoiceField, self).__init__(*args, **kwargs)
        self.fields['options'].choices = foo_choices
    options = forms.ChoiceField(label=False, choices=(), widget=forms.
RadioSelect(), required=True)

class SubmitButtonWidget(forms.Widget):
    def render(self, name, text, attrs=None):
        return '<button type="submit" name="%s" id="results" >%s</button>' % 
(html.escape(name), html.escape(text))

class ButtonField(forms.Field):
    def __init__(self, *args, **kwargs):
        if not kwargs:
            kwargs = {}
        kwargs["widget"] = SubmitButtonWidget

        super(ButtonField, self).__init__(*args, **kwargs)

    def clean(self, value):
        return value

class Button(forms.Form):
    def __init__(self, initial, *args, **kwargs):
        super(Button, self).__init__(*args, **kwargs)
        self.fields['save'].initial = initial
    save = ButtonField(label='', initial=())


# views
class Settings(View):
    template_name = "settings.html"
    def get(self, request):
        if request.session['mod'] == True:
            form = forms.SettingsForm(True, request.session['questionType'])
        else:
            form = forms.SettingsForm(False, request.session['questionType'
])
            
        submit = forms.Button('Save')
        return render(request, 'settings.html', {'form': form, 'submit':
submit})
    
    
    def post(self, request):
        if request.POST.get('mod'):
            request.session['mod'] = True
        else:
            request.session['mod'] = False
        if request.POST.get('choise'):
            request.session['questionType'] = request.POST.get('choise')
        redirect('/settings/')
        return redirect('/')
    

class Game(View):
    def get(self, request):
        all_question = Questions.objects.filter(category=request.session[
'questionType']).values()
        question = random.choice(all_question)
        answers = candidate_answers(question, all_question, request.session[
'mod'])
        if request.session['mod'] == True:
            form = forms.MyChoiceField(answers)
            button = forms.Button('Chek')
            return render(request, 'quiz.html', {'question':question, 'form'
:form, 'button':button, 'answer':answers[0][1]})
        else:
            button = forms.YesNoButton() 
            return render(request, 'quiz.html', {'question':question, 
'button':button, 'answer':answers[0]})
        
    def post(self, request):
        ok = request.POST.get('ok')
        answer =  request.POST.get('options')
        if request.session['mod'] == True:
            if ok == answer:
                answer = True
            else:
                answer = False
        else:
            if 'yes' in request.POST:
                if ok == answer:
                    answer = True
                else:
                    answer = False
            else:
                if ok != answer:
                    answer = True
                else:
                    answer = False
        return redirect('answer', answer = answer).py 

Is this a good django code, or i must 
or i must make form for every single form?
It is a good views or there a different way?


вторник, 19 април 2016 г., 15:45:55 UTC+3, Jani Tiainen написа:
>
> Hi, you should start with tutorials, 
>
> Django official: 
>
> https://docs.djangoproject.com/en/1.8/intro/tutorial01/ 
>
> Django Girls Tutorial: 
>
> http://tutorial.djangogirls.org/ 
>
> And finally on my list is Marina Mele's tutorial: 
>
> http://www.marinamele.com/taskbuster-django-tutorial 
>
>
> On 19.04.2016 14:23, Григор Колев wrote: 
> > I try to learnind django. 
> > But can't understand how to write good django code. 
> > Can I write form in template or thear a good reson to use django form. 
> > Or must I use class based view or function is the best idea. 
> > How can get the django form from value if post method in class based 
> viwe. 
> > 
> Въведете кода тук...
>
>
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7b5ca163-5d1b-4a89-8185-3b107563d4f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to