by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • May 16 2015 16:59
    olasitarska commented #362
  • May 16 2015 16:59

    olasitarska on master

    Making post_detail.html more co… Merge pull request #362 from ve… (compare)

  • May 16 2015 16:59
    olasitarska closed #362
  • May 16 2015 16:57
    kvbik commented #362
  • May 16 2015 16:51
    veuu opened #362
  • May 16 2015 15:20
    hjwp commented #361
  • May 16 2015 15:16
    hjwp synchronize #361
  • May 16 2015 15:15
    hjwp synchronize #361
  • May 16 2015 15:12
    bmispelon commented #361
  • May 16 2015 15:08
    hjwp opened #361
  • May 16 2015 14:58
    bmispelon opened #360
  • May 16 2015 13:15
    bmispelon commented #359
  • May 16 2015 13:15

    bmispelon on master

    Provide alternative runserver c… Merge pull request #359 from Al… (compare)

  • May 16 2015 13:15
    bmispelon closed #359
  • May 16 2015 13:14
    keimlink commented #359
  • May 16 2015 13:13
    olasitarska commented #359
  • May 16 2015 13:09
    bmispelon commented #359
  • May 16 2015 11:53
    Almad opened #359
  • May 15 2015 20:00
    bmispelon commented #358
  • May 15 2015 19:59

    bmispelon on master

    fixing broken github link Merge pull request #358 from gs… (compare)

Abhishek suresh
@abhishekkoothur_twitter
Thank you so much @das-g . That actually helped me and its fixed now. Thank you so much. I am really impressed with how much help this Community is.
+----+

I have a new doubt. Any hints are appreciated. So I have a bunch of buttons(called as topics) and a drop-down. I want to basically map them to blog posts. For example:- if I click on a button that says "Horror" and then click on the week dropdown and select "Week 1", it should show only blog posts that have Horror and Week 1 mapped to it(this I already did and can be selected in the admin).

Any idea on how to approach this? I tried to Google before asking here obviously but can't find a specific answer to it.

Lowe Wilsson
@datalowe
@chunboxing The local and pythonanywhere/"production" database aren't supposed to have the same content/posts, just like the tutorial says. You can create new posts on pythonanywhere if you want to have some posts to work with there.
Lowe Wilsson
@datalowe
@chunboxing For collectstatic, it would help if you described what error you get, and if you can run collectstatic locally or not
Lowe Wilsson
@datalowe
@ice_bear69, the tutorial you linked is probably fine, things haven't changed drastically since dec 2019. It would be more of a problem if you used something from before dec 2017, when django 2.0 was released if I'm not mistaken. I like this book, but it's not free https://djangoforbeginners.com/
@icebear, you removed your post so it's hard to tell what issue you had... remember to import forms in your views.py file
Lowe Wilsson
@datalowe

@abhishekkoothur_twitter So if I understand you correctly, you essentially want to create a search view. First you have to create a url path that's something like posts/<str:catname>/<int:weeknum>/'. Then you need a view which has a customget_queryset` method, using techniques similar to what's described in the search tutorial you went through earlier. E. g.

def get_queryset(self):
    return Post.objects.filter(category=self.catname).filter(week=self.week)

Then you want a form that uses the correct widgets for collecting inputs, as you want them. You can read about widgets here. I would guess you want RadioSelect and Select. Once you've done that, you write a 'search input' view, which uses the form and forwards the user to the custom queryset view (using the HTML <form>'s action attribute).

Something like that anyway :)

SnakeWhisperer
@SnakeWhisperer

Hello. I'm having some trouble when submitting a form that has several fields.

I have a 'Notes' model, which has a text field and other different fields, but the one I want to edit for now is the text field only. I created a ModelForm from the Notes model and I'm looping over all the table rows that have a note that is related to a specific project. What I want is to display all the notes at once and be able to edit whichever I want (one or more at once) and then submit them. However, when I hit the submit button after editing any of them, nothing happens, they are not changed; except when I edit the last one, which updates the database table, but puts that value in all of those rows.

Here is my form
image.png
Here is my view
image.png
here is my template
image.png
I don't think it's necessary to use save(commit=False) because I'm not doing anything with the data before saving it, but saving directly didn't change anything. I also tried to change where the submit button is, but nothing happened either. Why is this only submitting the last text field and putting that text value in all of the rows in my table?
Lowe Wilsson
@datalowe
@SnakeWhisperer To understand what is going on, try running your server and going to the page with the forms. Use your browser's dev toolkit's "Inspect element" functionality. Use this to look at the HTML that django has generated for your forms. I'm guessing you'll find that all your forms have the same name attribute (note_text, I would guess). When the user POST's their data, only the last form's note_text value is sent with the POST request, I think. It effectively "overwrites" all the other forms' note_text value. From your view's perspective, the view receives a POST dictionary that has a note_text key, paired with whatever value that was input for the last form on the page. So when you feed that dictionary of key-value pairs into the NoteForm class call, each time the same value is used.
To achieve what you actually want to do, I would guess that you should use formsets. I haven't used them myself so I don't know if the docs are beginner-friendly on this subject, or if you're better off googling for a tutorial to understand how you can use them. Hope it helps anyway :)
If you want then please write something short later on if you do use formsets and make things work, it would help me learn too
SnakeWhisperer
@SnakeWhisperer

When the user POST's their data, only the last form's note_text value is sent with the POST request, I think. It effectively "overwrites" all the other forms' note_text value. From your view's perspective, the view receives a POST dictionary that has a note_text key, paired with whatever value that was input for the last form on the page. So when you feed that dictionary of key-value pairs into the NoteForm class call, each time the same value is used.

@datalowe That's exactly what's happening, and I don't really know how to access a specific element's data from my view. When modifying an existing model instance, I thought I could access it's pk, but I haven't been able to find how to do that—or if it would actually work. Although, when adding a functionality to add a new note, there would be no pk before it's saved to the database.

I'll check the formsets and see what I can do. Thank you very much. Great as always.

SnakeWhisperer
@SnakeWhisperer

@datalowe I was able to make my forms work with formsets, specifically with model formsets. This is because I am editing model instances. My current task is very simple, but I—and anyone else reading this—can build from here to more complex things.

I'm not updating anything on my forms.py file because, with this approach I ended up not using it.

Here is my view

image.png
SnakeWhisperer
@SnakeWhisperer
I've added extended comments to show what's happening. It's possible that I misunderstood something; I'm doing this for the first time. Also, note that, to create the formset, I'm using a queryset that filters the objects by their project_id; this is why I'm not using the forms.py file and doing everything here in the view.
This is my HTML
image.png
Lowe Wilsson
@datalowe
Good job! You don't really need to specify the exclude argument if you're using fields. At least that's the case when using ModelForm, and I would expect django to be consistent with that for modelformsets. I've heard it recommended to actually always use fields, and never rely on exclude. The reasoning is that if you add additional attributes/fields to a Model, if you're using exclude, then form fields for those new attributes will automatically be included in generated forms, even though those attributes should perhaps be hidden/not possible to edit. When using fields, forms will always only use fields the dev has specified. This is something that confused me quite a lot so I thought I'd comment on it.
Lowe Wilsson
@datalowe
Instead of using len(Note.objects.filter(...)) == 0 you can use not Note.objects.filter(...).exists() (or you can flip the if/elses if you want to drop the not). django docs on .exists. This probably doesn't make a big difference here but the syntax is a bit cleaner so it's probably a good habit to use .exists in cases like this. It's supposedly a bit more effective in some cases, too.
matrixbot
@matrixbot
ice_bear69 Fuck
ice_bear69 Fucking hell
ice_bear69 I don’t fucking get how forms work
SnakeWhisperer
@SnakeWhisperer

Good job! You don't really need to specify the exclude argument if you're using fields. At least that's the case when using ModelForm, and I would expect django to be consistent with that for modelformsets. I've heard it recommended to actually always use fields, and never rely on exclude. The reasoning is that if you add additional attributes/fields to a Model, if you're using exclude, then form fields for those new attributes will automatically be included in generated forms, even though those attributes should perhaps be hidden/not possible to edit. When using fields, forms will always only use fields the dev has specified. This is something that confused me quite a lot so I thought I'd comment on it.

Yeah, if I'm leaving it empty, I though I might as well not specify it.

Instead of using len(Note.objects.filter(...)) == 0 you can use not Note.objects.filter(...).exists() (or you can flip the if/elses if you want to drop the not). django docs on .exists. This probably doesn't make a big difference here but the syntax is a bit cleaner so it's probably a good habit to use .exists in cases like this. It's supposedly a bit more effective in some cases, too.

You know, I realize it's the first time another programmer comments on the style of any of my code, apart from comments on its functionality, of course. I appreciate your suggestion. I see QuerySets kind of cover a lot and it's not a piece of cake to just find the right way to get what you actually need.

Tejaswi
@Tejaswigilla
I'm not getting total result on public server but I'm getting it on local server.
Can you please help me so that I can do it
how to upload screenshots so that it will be easy for you to give me an advice
SnakeWhisperer
@SnakeWhisperer
@Tejaswigilla Hi. Just copy and paste the screenshot here. Note that, as soon as you paste it, it will be sent; you don't have to press return (or enter) after it.
SnakeWhisperer
@SnakeWhisperer
Does anyone know how to use AJAX with Django to perform some action when a button is clicked? I'm reading this https://realpython.com/django-and-ajax-form-submissions/ , but I'm kind of lost. Actually, this made me realize I'm not so clear about how the views handle the requests. What I want to do is have a button that makes my formset have a new empty form (an "add note" button) but without reloading the page. Now, I see that I'll have two different kind of requests for the view to handle; one will submit the forms and the other will be that from the button to create a new empty form. How would my view know which request is the one sent?
JpabloM
@JpabloM
I have an error that say.
NameError: name ‘os’ is not defined
matrixbot
@matrixbot
th5312 import os
AI_mAIson
@whoosher
can I ask some Question? 질문 해도 될까요?
Raphael Das Gupta
@das-g

Hi @whoosher

If you have questions, just state them. You don't need to ask for permission first. :smile:

Markita
@MarkitaMolo_twitter
Hi All, I work for an NGO in the US that supports women in subsidized housing in building assets. We built an online platform of practitioners in our field called FSS Link. It's Django powered and we're looking for a new development firm. I would love to engage and explore firms owned or lead by women or at least employing decent numbers of women engineers. Any suggestions?? I refuse to hire from all male firms. Thanks in advance.
aman jha
@amanjha18
how can I learn django fast ? I have basic knowledge of python.
Abhishek suresh
@abhishekkoothur_twitter
Our very own Django Girls tutorial is the best way to learn Django @amanjha18 . https://tutorial.djangogirls.org/en/
Nguyen Gia Bao
@baobaoack000
Nice
quic question , how do i make helloworld in django
@abhishekkoothur_twitter
Raphael Das Gupta
@das-g
If you combine the chapters Django installation and Your first Django project!, the result is more or less what could be considered a "Hello World!" (I won't yet display your own "Hello" message, but a generic welcome page.)

If you're new to programming in general, to python programming in particular and/or to web development, I'd really recommend to go through the whole Django Girls tutorial.

If you're already familiar to both Python and web development and sufficiently comfortable and confident in both, the tutorial in the official Django documentation might be more suitable.