How to create and edit a post

How to create a new post but without using django generic views. I used that syntax to show the list of information and the detail:

views.py /

def list(request):
    val = Car.objects.all()
    context = {'val': val}
    return render(request, 'post/index.html', context)

def detail(request, idx):
    detail = Car.objects.filter(id=idx)
    context = {'detail': detail}
    return render(request, 'post/detail.html', context)

Now, how to create a new post and edit it? I’m using the same syntax that I used in django shell, to get a list of objects and details, but I don’t know how to use the same syntax to create and edit a post. Any tips or ideas will be very welcome. I’m just trying to create a CRUD, but without using the generic views.

Thanks :slight_smile:

In general, the way to do this will be to create a Form, and a View that renders the form and handles the POST from the form.

Start with reviewing the Working With Forms page.

Read through the examples until you understand what they’re doing. Then try doing the comparable functions with your own model. If you run into any problems, feel free to come back and add to this thread.

1 Like

Thank you for the answer.

I already did that method, using the Form class, and using the jinja tag to create the form, but, my problem with that method, is that I can not edit the text box, I mean, I am not able to put, for example, a bootstrap style on that form. In html, using the input tag, I am able to do it.

It is possible edit the text box using the Form method?

Thank you again.

You mean to apply a css style to a text box?

See the docs on Styling Widget Instances.

1 Like