2

Suppose I have a 'question' model with two fields - title and content

Now think of question creation form. Using django forms I can display it to template and validate using is_valid method.

But I don't want to use django inbuilt forms. I want to write my forms in handwritten html and send it to server using POST method.

How will validation and saving to database will work in this case?

I am doing something similar to this guy but I am not satisfied with the existing answers

abcDE
  • 63
  • 1
  • 7
  • Why make more work for yourself? You can still render fields manually. See the section [Rendering fields manually](https://docs.djangoproject.com/en/3.1/topics/forms/#rendering-fields-manually) in the documentation. You can also add a few attributes to the field by changing the fields definition in the form. If you still don't like doing that you can always use these great packages to customize rendering of fields [django-widget-tweaks](https://pypi.org/project/django-widget-tweaks/) and [django-crispy-forms](https://django-crispy-forms.readthedocs.io/en/latest/) – Abdul Aziz Barkat Mar 07 '21 at 15:47
  • @Abdul Aziz Barkat Actually I want to use a markdown editor instead of django forms textarea to get the input from user. Kind of like stackoverflow's editor when answering questions. Django defaults to textarea and my markdown editor is a third party JS package. – abcDE Mar 07 '21 at 15:53
  • That just implies you need a custom widget. Check this question [Django: How to build a custom form widget?](https://stackoverflow.com/questions/4707192/django-how-to-build-a-custom-form-widget) – Abdul Aziz Barkat Mar 07 '21 at 15:57
  • I'd recommend creating the form, not use it for the html presentation if you don't want to (although I'd probably build a widget), and utilize the forms validation and benefits of sanitization. – AMG Mar 08 '21 at 04:05

1 Answers1

0

In your views:

import Model
Param = request.post.get('param') 
Model.objects.create(field=param)

Just like that, you have saved that to the model.

Chymdy
  • 662
  • 4
  • 14
  • 1
    That's not a very great method. You are losing out on cleaning and validation of the data. The OP's question mainly was _"How will validation and saving to database will work in this case?"_ – Abdul Aziz Barkat Mar 07 '21 at 16:26
  • I see your suggestions and they are good. But he says he doesn't want to use django forms. – Chymdy Mar 07 '21 at 16:32