Help with django.

28 views
Skip to first unread message

Григор Колев

unread,
Apr 19, 2016, 12:02:30 PM4/19/16
to Django users
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.

Jani Tiainen

unread,
Apr 19, 2016, 12:45:55 PM4/19/16
to django...@googlegroups.com
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
Message has been deleted

Григор Колев

unread,
Apr 19, 2016, 9:06:09 PM4/19/16
to Django users
I apologize for the mistakes, but function for insert a code is just terrible

Григор Колев

unread,
Apr 19, 2016, 9:21:06 PM4/19/16
to Django users


сряда, 20 април 2016 г., 0:00:06 UTC+3, Григор Колев написа:
)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.py
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')
       
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)

Is this a good django code, or i must
make form class for every single form?
It is a good views or there a different way?
Въведете кода тук...



Reply all
Reply to author
Forward
0 new messages