Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Django add/edit form not returning initial/default value set while initialisation

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Raman Tripathi

User

( 8 months ago )

 

I have view which I use for both adding and editing a modelform. Edit form works fine but while loading the add form, the initial values which I had set through Python code is not showing.

Views.py(1):

def tracker_edit(request, track_id=None, template_name='posts/tracker_edit.html'):
    if track_id is not None:
        track = get_object_or_404(Post, pk=track_id)
    else:
        track = Post()
    tracker = CreateArticle(request.POST or None, instance=track)
    if request.POST and tracker.is_valid():
        tracker.save()
        redirect_url = reverse('posts:tracker_list')
        return redirect(redirect_url)

    return render(request, template_name, {
        'tracker': tracker
    })

Forms.py:

    Day_Of_Week=(('Weekday','Weekday'),('Weekend','Weekend'))
    weekno = datetime.datetime.today().weekday()

    if weekno<5:
        x = ("Weekday")
    else:
        x = ("Weekend")

    Day_Of_Week = forms.ChoiceField(choices=Day_Of_Week, widget=forms.RadioSelect, initial=x)

For example, from the forms.py you can see I'm expecting the radio button to be auto selected as weekday or weekend. But none is selected. Im confused, am I doing anything wrong with get/put? Any fix for the above will be appreciated..

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif