Live data from Hacker News

Learn web development: Django Web Framework

developer.mozilla.org

31–40 of 54 posts

Re: Learn web development: Django Web Framework

#31
post #25

It has been years since I have used Django - do they still expect everything in the model to go in a single file or can you put each model in it's own file now?

Create a models.py and import there your models from wherever they are. This should do the trick.

Yeah, or another way of splitting up huge models files is to convert models from a module to a package by making a directory models and adding models/__init__.py, models/foo_models.py, models/bar_models.py then doing

from .foo_models import *

from .bar_models import *

in your __init__.py

Re: Learn web development: Django Web Framework

#32
post #11

I had used Django back in version 1.3 and was just thinking today of how I need to brush up, as it has been a few years. The timing of this article couldn't have been better. Now I've got my weekend cut out :)

Everything about Django 1.8 and above blows everything below away. Its all the same, but so much better. The admin app keeps getting better, migrations are a first class citizen and make ongoing change management and deployment a snap. If you want to make a solid, stable web-app Django is a perfect backend.

This is so true. Point in case - Instagram also runs on Django!

Re: Learn web development: Django Web Framework

#33
post #5

Earlier quoted context omitted.

Django's basic tutorial is great to get started, but once you get into the nitty gritty I find myself often going to stacks rather than the docs to get the information I need because the docs get kinda sparse. One example is if you want to forgo modelforms and pass the information from the forms to the model manually through your view (for example because you want to manipulate the information before passing it to th…

How did you solve the problem of bypassing modelforms? Same roadblock here...

you reference the model in the view and then tell the view to save it directly to the model using the "model = form field" type syntax to assign value and then .save() method to save.

In the form field you are saving from, you write the form field value and then an empty space where the input value goes sort of like "form.cleaned_data.get("field name", " "). The second blank ""is the empty space where the field value goes. The claned_data is django's internal form validator. That sounds confusing so I'll give an example

This is what your views would look like

    from django.shortcuts import render

    from sample.models import Test

    from sample.forms import TestForm

    def Example(request):

       if request.method == 'POST':

           form = TestForm(request.POST)

           if form.is_valid():

              testmodel = Test()

              testmodel.modelfield1 = form.cleaned_data.get('formfield1','')

              testmodel.modelfield2 = form.cleaned_data.get('formfield2','')

              testmodel.save()
            
              return render(request, 'sample/nextpage.html') 

        else:

            form =TestForm

            context_data = {'form':form}

            return render(request, 'sample/testformpage.html', context_data)

Re: Learn web development: Django Web Framework

#34

If you're curious about how to set up a modern JS frontend on top of Django, I wrote up a tutorial recently. It might be helpful if you're debating between trying a Single Page App approach or some other design: https://medium.com/@theSquashSH/reconciling-djangos-mvc-temp...

To easily make a modern feeling frontend, whilst taking advantage of Django's backend, and without taking on all of the complexity of frontend SPA focused frameworks, I can highly recommend IntercoolerJS: http://intercoolerjs.org/

Clearly it depends what your use-cae is, but for a large class of apps that you'd build with Django it feels just right for me: http://intercoolerjs.org/2016/10/05/how-it-feels-to-learn-in...

Re: Learn web development: Django Web Framework

#35
post #24

If you're curious about how to set up a modern JS frontend on top of Django, I wrote up a tutorial recently. It might be helpful if you're debating between trying a Single Page App approach or some other design: https://medium.com/@theSquashSH/reconciling-djangos-mvc-temp...

I think one important lesson here is that by abandoning the traditional SPA approach, you're also opening up your site to be indexed by search engine crawlers much more easily.

this is something i struggle to understand. especially with SSR/isomorphic rendering.

the vast majority of websites that need indexing should not need JS to create the html.

the vast majority of web apps that benefit from being SPAs are behind some form of login/auth and so do not need indexing.

yet there's this obsession with writing SPAs that can be pre-rendered for better indexing. i only assume this is a solution to the desire of using nodejs and javascript as a server-side language regardless of merit.

Re: Learn web development: Django Web Framework

#36

If you're curious about how to set up a modern JS frontend on top of Django, I wrote up a tutorial recently. It might be helpful if you're debating between trying a Single Page App approach or some other design: https://medium.com/@theSquashSH/reconciling-djangos-mvc-temp...

The tutorial mentions server side rendering is easier using this approach, but doesn't mention how it is done.

Since the html output is minimal, presumably you require something like python-react that pipes the output through a nodejs server?

Presumably building the same in a React SPA using Django, that would also mean maintaining the routing system in two different frameworks at once?

Re: Learn web development: Django Web Framework

#37
post #33

Earlier quoted context omitted.

How did you solve the problem of bypassing modelforms? Same roadblock here...

you reference the model in the view and then tell the view to save it directly to the model using the "model = form field" type syntax to assign value and then .save() method to save. In the form field you are saving from, you write the form field value and then an empty space where the input value goes sort of like "form.cleaned_data.get("field name", " "). The second blank ""is the empty space where the field value…

"The second blank ""is the empty space where the field value goes."

I'm not sure I understand what you're saying here, but I don't think this is right. cleaned_data is a dictionary, and so the second argument to get is a default value to use if there is no value corresponding to the key. So, in your example, if there's no value for "formfield1" in cleaned_data, it would return a string with a space in.

This is probably not what you want to do. cleaned_data will always contain values for declared form fields, so the only way the default would be used is if you've made a mistake in specifying the key (for instance, if there's a typo and you've written 'formfeild1' instead of 'formfield1'). If you've made that kind of mistake, you don't want the program to continue using a single space instead of the valid data (you'll lose data) - you want the program to report that there's something wrong.

So I think you should probably use

    form.cleaned_data['formfield1']
which will do the same thing if formfield1 is a real field name specified in the form, but will raise an exception if it isn't.

Re: Learn web development: Django Web Framework

#38
post #33

Earlier quoted context omitted.

How did you solve the problem of bypassing modelforms? Same roadblock here...

you reference the model in the view and then tell the view to save it directly to the model using the "model = form field" type syntax to assign value and then .save() method to save. In the form field you are saving from, you write the form field value and then an empty space where the input value goes sort of like "form.cleaned_data.get("field name", " "). The second blank ""is the empty space where the field value…

Alternative is to use the modelform but not save the created/updated model automatically. Then you modify the resulting model object and save it manually as needed.

Works also with models with m2m fields with Form.save_m2m().

Re: Learn web development: Django Web Framework

#39
post #34

If you're curious about how to set up a modern JS frontend on top of Django, I wrote up a tutorial recently. It might be helpful if you're debating between trying a Single Page App approach or some other design: https://medium.com/@theSquashSH/reconciling-djangos-mvc-temp...

To easily make a modern feeling frontend, whilst taking advantage of Django's backend, and without taking on all of the complexity of frontend SPA focused frameworks, I can highly recommend IntercoolerJS: http://intercoolerjs.org/ Clearly it depends what your use-cae is, but for a large class of apps that you'd build with Django it feels just right for me: http://intercoolerjs.org/2016/10/05/how-it-feels-to-learn-in.…

Not to go too far off topic, but I wish intercooler were just built into html6 or bootstrap. It's full of all the most common things that you wish you didn't need to write a full js app to do.

Re: Learn web development: Django Web Framework

#40
post #24

Earlier quoted context omitted.

I think one important lesson here is that by abandoning the traditional SPA approach, you're also opening up your site to be indexed by search engine crawlers much more easily.

this is something i struggle to understand. especially with SSR/isomorphic rendering. the vast majority of websites that need indexing should not need JS to create the html. the vast majority of web apps that benefit from being SPAs are behind some form of login/auth and so do not need indexing. yet there's this obsession with writing SPAs that can be pre-rendered for better indexing. i only assume this is a solution…

Some sites are a combination of content (blog, products etc) and a login area with a complex UI. A SPA works well for sophisticated user interfaces but is overkill for content that is just for reading. But then you have to decide is it worth jumping back and forth between JS and your server side language (Java/Ruby/Python) or maybe it would be nice to just have one language to rule them all...
Post reply on HN