What is views.py for? - django

I am currently learning how to build a backend and I have only used the views.py file to set up Django Rest Framework. Almost everything is happening in my models.py or admin.py file.
Is that a problem? What even is views.py for?
I should add that I will be using Angular for the frontend later!

The convention is you that you write the logic that connects to your db in views.py but then you can define the view anywhere you prefer even inside the models.py so long as you're able to call it when needed.
For more, check.
this here

Django works on MVC model which enables to structure data and template individually.
In django we retrieves the data from the models in views and render that data with a template. So view processes the data with our logic and give that data to template.

Related

On what level in the django MTV architecture does the Django Rest Framework work?

I understand that django is an MTV architecture. I also understand that M is models, T is templates (which are views in MVC) and V is views (which are controllers in MVC).
I want to understand if the django serializers and views are views or templates according to the MTV when using Django Rest Framework.
There's no perfect 1:1 mapping there.
A DRF serializer is vaguely like a template in that it describes how the data should be serialized "onto the wire" when a DRF view outputs it, but unlike templates, serializers can also be used for data ingestion.
DRF views (and viewsets, which group views) map to MVC controllers or Django views in that they describe how the API endpoint works.
A DRF app doesn't necessarily need to work with Django models at all, but if it does, that bit is unchanged from how Django works.

Build react form based on serializer of Django Rest Framework

I want to build web app where users can add different ads for sale(cars,apartments, gadgets etc...) and each of this categories models in django are specific and need specific fields in form for creating ad. Creating this forms using django is easy by using ModelForms.
DRF documentation says that
A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields..... . So I guess I will create form on base of serializers.
My question is: How make a single React component which will render form on base of different serializers that frontend gets from Django-Rest-Framework?
Please any advice or links where I can read about this . If you need I can share some code . Thank you in advance.
Quite a late response, but here's some workaround.
You could check this npm package that convert JSON into React Form: https://www.npmjs.com/package/react-jsonschema-form
You could generate the JSON description from your Django model, by looping among attribute.
It's not straightforward but it worked fine for me.

Django - can someone explain saving model info to a db works in a MVC framework?

So I have a model class a form class and a views class.
I am having a hard time effectively explaining to someone how it works in the sense of a MVC framework. I am new to django and I have followed the documentation however, i cannot seem to explain it well enough.
To my understanding you create the model, which you pass into your form to create a form, then pass that into your view when you render onto an html page through context then you wait for a user to hit a submit button which returns the info via POST and then you catch that data in your view and go through the necessary steps to save the data. Am I missing anything?
In python models.py and the forms together are the controller in MVC since they contain the program logic and control saving objects in the DB, views.py file is showing the content of data, so it's the View. The Model is what django ORM handles for us. It stores the data and handles load/stores in the DB.

How to use admin interface if I have no application?

I am creating a Django based app and I'd like to put everything under the root in the following structure:
/path/to/my/app/
settings.py
models.py
urls.py
admin.py
...
One problem that I run into is the admin interface doesn't include whatever models I have that are registerd in admin.py usin
admin.site.register(models.MyModel)
Usually that's done by using auto discover in urls.py, but now I have no registered "app", the auto discover doesn't work anymore. Is there anyway I can still use the admin interface?
Thanks.
Django simply doesn't work without apps. They're the fundamental building block of a Django site. A whole range of things, not just the admin, will fail to work. Why do you want to do this?
Putting the app in the django-style directory structure will make your project easily extensible if you decide to add functionality later.

Django form to enter/save html to database

I'm in my first week of Django development and am working on an admin page that will let me write some quick html using TinyMCE and then save it to the database. I don't need to display this web page on the site or add it to urls.py, etc. The html snippet will be loaded from the database and used in a view function.
I've read in "Practical Django Projects" how to integrate TinyMCE, so my question is more concerned with the best approach for the form itself. Specifically:
1. Is there a built-in form like flatpage that works well for this? I only need one field in the form for the html.
2. How do I save the form's text after it's entered?
I created a model with a JSONField to save the html in, but I'm not clear on what to do next. Thanks.
here is the documentation for Django Flatpages App, maybe you serve.
I ended up using the ModelAdmin class to get what I wanted. I created a new model in models.py and then used ModelAdmin to enable an admin-editable form for the model's data.
Hope this helps someone.