Live data from Hacker News

Learn web development: Django Web Framework

developer.mozilla.org

41–50 of 54 posts

Re: Learn web development: Django Web Framework

#41

Earlier quoted context omitted.

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

Separating models like this is a hint that you may need another app. It has happened numerous times with me now that my models.py gets large and unwieldy, I spends a hour breaking it all out and then I realise my one big God app needs to be 3 smaller ones.

Re: Learn web development: Django Web Framework

#42
post #5
post #2

Great to see Mozilla supporting Django. But isn't the official Django tutorial cover all of this, Why not just link to it?

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…

You should consider contributing to the docs if there are things you think could be clearer or more thorough. It's a great way to get your feet wet in OSS, and really helps the next person.

Re: Learn web development: Django Web Framework

#43
post #7

Dumb question, why does the guide suggest that you create an empty folder and then initialize the project from there? (django_test/mytestsite, locallibrary/locallibrary) As far as I can tell, no file, hidden or otherwise, is left in the outer folder.

I'm not sure if they do this in the tutorial, but it's a convention that some people use to have the virtual environment next to the project folder in that parent folder so that you don't have to gitignore it (just have the repo in the project folder).

Re: Learn web development: Django Web Framework

#44

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...

Well, this is exactly what I was looking for. I am still a pretty new, self taught developer, and during my trials and tribulations I fell in love with python and django. Now it seems React is the cat's meow, so I have started to look into it and was wondering how it could work well with Django. Than k you

Re: Learn web development: Django Web Framework

#45
post #37
post #33

Earlier quoted context omitted.

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 probab…

Sorry typo, it should be the second blank is the empty space where the user input goes.

I just tried your solution with cleaned_data["formfield1"] instead of cleaned_data("formfield1","") and it didn't work for me, it came back with the following error message when I submitted the form:

>'builtin_function_or_method' object is not subscriptable

I think you have to have the empty quotes after the form field and I think that's what's capturing the user input

so here "form.cleaned_data("formfield1","") seems to be telling django the form name and then the second field is where the associated user input goes which is then passed to the model.

Re: Learn web development: Django Web Framework

#46

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...

This is a great idea. I'm currently using the SPA + granular API method for my Angular + Django projects. It's working well enough, but your method seems much more elegant, I can already imagine the benefits. We also have a team currently looking for the best way they can integrate PHP and React, and I'm going to share this with them. Thank you!

Re: Learn web development: Django Web Framework

#47
post #45
post #37

Earlier quoted context omitted.

"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 probab…

Sorry typo, it should be the second blank is the empty space where the user input goes. I just tried your solution with cleaned_data["formfield1"] instead of cleaned_data("formfield1","") and it didn't work for me, it came back with the following error message when I submitted the form: >'builtin_function_or_method' object is not subscriptable I think you have to have the empty quotes after the form field and I think…

wait just tried the code again, you were right, I don't need the second empty string. I was using [] with get and it wasn't working.

so it seems the right way is form.cleaned_data.get('formfieldvalue')

or

form.cleaned_data[formfieldvalue']

if you don't want to use get

Re: Learn web development: Django Web Framework

#49

Earlier quoted context omitted.

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

Separating models like this is a hint that you may need another app. It has happened numerous times with me now that my models.py gets large and unwieldy, I spends a hour breaking it all out and then I realise my one big God app needs to be 3 smaller ones.

Totally agreed.
Post reply on HN