Reputation: 139
I have just started learning Django and I am having confusion regarding the architecture of a django project.
Basically what I want to know is the recommended way to design a django application ie: what type of code do I put in the models file, the views file and where do I write the validators etc.
As an example, suppose that while creating a registration form to add a new user I want to make sure that the user does not register with a username that is already present in the database. As per my observation there are three ways to do it.
As a beginner I am confused as per what approach would be best. So a basic set of rules to follow that can help me decide what type of code is written where will greatly help me. Thanks
Upvotes: 1
Views: 983
Reputation: 37003
Unsurprisingly, the general recommendation would be to put your view code in views.py
, your model code in models.py
and your form code in forms.py
.
You have the ability to put code more or less wherever you want it, but you are better off sticking with these recommendations as a beginner.
Since you want to be sure that an added user isn't already in the database that would best be handled in the view code, but there's nothing in principle wrong with using a model method to check new save()
s for duplication. It's a matter of whether the functionality is required anywhere else.
Matters of application architecture can be difficult for newcomers. The recommendations in the book "Two Scoops of Django" embody many best practices.
Upvotes: 1