def create_topic(request):#the action of posting the new contact to database. Should create a modelform in model.py. if request.method == 'POST': form=Topic_Form(request.POST)#create data to database if form.is_valid(): title = form.cleaned_data['title'] text = form.cleaned_data['text'] form.save(commit=True)#have to save, the data will be uploaded return index(request)#go back to the main page return index(request)
def update(request, topic_id):#post modified data to the current contact topic = get_object_or_404(Topic, pk=topic_id) if request.method == 'POST': form=Topic_Form(request.POST, instance=topic)#edit current contact data in database if form.is_valid(): topic.title = form.cleaned_data['title'] topic.text = form.cleaned_data['text'] form.save(commit=True) return index(request)#go back to main page return index(request)