I am new in Django framework and trying to understand how it works and what is its structure. I am just curious about how a django form is passed to a html template ? Any help would be appriciated.
I think you are curious in how django converts a python object into HTML, its internal mechanism, so the tutorial might not cover what it is.
I did have the same question before, but if you do look at code in forms.py https://github.com/django/django/blob/master/django/forms/forms.py you will see that internally there are methods which will look at the attributes of the objects that you have declared and generate snipplets of html code which will then be rendered.
Of course I cant tell you exactly how it works, I leave the heavy lifting to django.. Isn't that why we use a modern web framework like django in the first place.
Hope you will find this useful
Related
I was wondering that whether Django is a MVC or MVT framework? I searched this question on net but didn't find any suitable or satisfactory answer.
I found a partial answer to this question directly in Django's FAQs
https://docs.djangoproject.com/en/3.1/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names
Quoting directly here for convenience:
Django appears to be a MVC framework, but you call the Controller the “view”, and the View the “template”. How come you don’t use the standard names?
Well, the standard names are debatable. In our
interpretation of MVC, the “view” describes the data that gets
presented to the user. It’s not necessarily how the data looks, but
which data is presented. The view describes which data you see, not
how you see it. It’s a subtle distinction. So, in our case, a “view”
is the Python callback function for a particular URL, because that
callback function describes which data is presented. Furthermore, it’s
sensible to separate content from presentation – which is where
templates come in. In Django, a “view” describes which data is
presented, but a view normally delegates to a template, which
describes how the data is presented. Where does the “controller” fit
in, then? In Django’s case, it’s probably the framework itself: the
machinery that sends a request to the appropriate view, according to
the Django URL configuration. If you’re hungry for acronyms, you might
say that Django is a “MTV” framework – that is, “model”, “template”,
and “view.” That breakdown makes much more sense. At the end of the
day, it comes down to getting stuff done. And, regardless of how
things are named, Django gets stuff done in a way that’s most logical
to us.
Django is an MVT based framework. And in that “M” stand for Model “V” stands for View & “T” stands for Template
Model: The Model is the logical data structure behind the entire application and is represented by a database(generally relational databases such as MySql, Postgres).
View: View is the main functionality part of Django architecture, where we write the business logic which is going to be responsible for request and response according to the client inputs.
Template: By the name itself it's showing its behavior. The template is the part that is used for the representation of HTML pages on the web browser.
If you want more specific detail about django mvt architecture, you can refer to this article which I found good and they have explained very well with a diagram representation Django MVT Architecture
Django follows MVC pattern very closely but it uses slightly different terminology. Django is essentially an MTV (Model-Template-View) framework. Django uses the term Templates for Views and Views for Controller. In other words, in Django views are called templates, and controllers are called views. Hence our HTML code will be in templates and Python code will be in views and models.
A complete explanation of this can be found here - https://overiq.com/django-1-10/mvc-pattern-and-django/
Djnago follows MVT framework.M=model V=views. T=templates
https://www.geeksforgeeks.org/django-project-mvt-structure/
I have been facing a big challenge with django since I was assigned the task to rebuild the admin page of our platform. I started unsure of what to do and now that I am half way on, I am even more unsure.
Here is my question:
If I want to build a highly customized admin, should I do a giant hack on the source code or create a new app?
1) Giant Hack
That is how I started and got stuck. A couple of problems appeared on the way, for instance:
I had to extend the AdminSite and override every view I wanted to change
Django's admin is very modular (I mean, very very modular) and a screen might be the sum of tons of other templates
Creating a simple navbar is a pain in the ass (at least it has been for a newbie like me). This is due to the fact that app_list (the variable that contains the mapped models to show in admin's first page) not being accessible when other templates rather than base.html are injected. Thus, I would have to create a context processor, replace every TemplateResponse() by render() (technically, since I tried to do it and it didn't work as well)
The problems go on and on. Given my lack of experience with django, I might be doing crap, but it does feel like I am on the wrong way.
this guy's answer motivated me to post this.
2) Building a brand new app
Of course it will take a little while to implement this, but it seems like a robust and maintainable way of getting it done. One of the points is that I will need to give the same flexibility as the default admin site gives when adding models and promptly having their cruds and tables (I am not really sure how to accomplish this behavior).
I am pretty new to django and any guidelines will be highly appreciated. Give me your thoughts on how to make it.
There is no need to overwrite admin views. The easiest way is to overwrite the templates and add your own css & javascripts.
I have added my own navbar, sidebars and bottom. It is really easy if you overwrite the admin base templates.
If needed you can provide custom data to the template by using own templatetags.
I would like to have a custom view of login and register forms in my views, without loosing the functionalities of SQLFORM. How can I change the "look" of the forms in views. Please give me some pointers like which CSS file I should be modifying. I donot have knowledge on HTML and CSS, hence I am asking your help.
Thank you so much
A thinker :)
First, you can alter the general HTML structure of forms by using the formstyle argument to SQLFORM. It takes values, "table3cols", "table2cols", "divs", "ul", "bootstrap", "bootstrap3_stacked", and "bootstrap3_inline", and it can also be a function that generates a custom structure (for an example, see the gluon.sqlhtml.formstyle_bootstrap3_stacked function). For Auth forms, you can set the formstyle via the auth.settings.formstyle setting (see here).
You can modify the CSS for forms however you like. Look here for relevant details. You may also find it useful to generate a form and then examine the generated HTML source code in your browser to understand the structure of the form. If you are using the "welcome" app as your foundation, the relevant CSS files that currently impact form appearance are skeleton.css and web2py.css in /static/css (as of version 1.99.3, which was just released today).
If you need to do more than can be handled via the formstyle option and CSS changes, then you can create a completely custom form in HTML. See here and here for details.
If you have specific questions as you proceed, feel free to ask for help on the mailing list.
I am learning Django and got it to work with wsgi. I'm following the tutorial here:
http://docs.djangoproject.com/en/1.1/intro/tutorial01/
My question is: how can I customize the look and feel of Django? Is there a repository of templates that "look good", kind of like there are for Wordpress, that I can start from? I find the tutorial counterintuitive in that it goes immediately toward customizing the admin page of Django, rather than the main pages visible to users of the site. Is there an example of a "typical" Django site, with a decent template, that I can look at and built on/modify? The polls application is again not very representative since it's so specialized.
any references on this would be greatly appreciated. thanks.
Search for generic CSS/HTML templates, and add in the Django template language where you need it. Because unless you are trying to skin a particular app (such as the admin system), there is nothing Django-specific about any of your HTML.
The fact that you're thinking in terms of Wordpress templates, and that you think the tutorial's poll application is highly specialised, are hints that you haven't really grasped what Django is. It isn't a content management system or a blog engine, although it can be used to build those things.
There's no such thing as a typical Django site, and it simply doesn't make sense to have pre-packaged templates, because the front end could be absolutely anything at all - like a poll.
You write the template like you would write any standalone HTML+CSS page, perhaps with placeholders for the content, then turn those placeholders into actual Django template tags. If you know how to do write HTML, then you know how to make a Django template.
Actually Django does not have a "look and feel". You are probably referring to the built in Django Admin application. That app comes with its own templates.
There are third party applications that can change the Admin interface, Django Grapelli is a great example.
For any other application you want to build yourself, or download. Most likely you'll have to do the templates yourself. In order to come up with something pretty you need to learn about CSS/HTML/JS and design principles as the Django Templates will quite likely be out of your way.
I always recommend HTML Dog for learning the basics of HTML, CSS and JS.
I've been developing in the Symfony framework for quite a time, but now I have to work with Django and I'm having problems with doing something like a "component" or "partial" in Symfony.
That said, here is my goal:
I have a webpage with lots of small widgets, all these need their logic - located in a "views.py" I guess. But, how do I tell Django to call all this logic and render it all as one webpage?
It sounds like what you're looking for is something like custom template tags...
You can write your own set of tags that process custom logic and return template chunks that are reusable in a very widget-like way.
Assuming you are going to be using the components in different places on different pages I would suggest trying {% include "foo.html" %}. One of the (several) downsides of the Django templating language is that there is no concept of macros, so you need to be very consistent in the names of values in the context you pass to your main template so that the included template finds things it's looking for.
Alternatively, in the view you can invoke the template engine for each component and save the result in a value passed in the context. Then in the main template simply use the value in the context.
I'm not fond of either of these approaches. The more complex your template needs become the more you may want to look at Jinja2. (And, no, I don't buy the Django Party Line about 'template designers' -- never saw one in my life.)