Hello,
I am new to Django and have been working on a personal project, I do not know where to post my code or actual project for a person to see, I do have it on GitHub but am unsure about the rules of the forum. If someone can comment, I am happy to delete this topic and post a new one with a better format.
The main issue is that I am having trouble creating a text box in the actual website where you can put multiple IP addresses in that text box. Essentially what I want it to do is that it will be able to read 0.0.0.0, 0.0.0.0, 0.0.0.0 and add those three to the json file I have made already. I have also written a piece of code to verify addresses with brakcets, i.e 0.0.0[.]0 and remove those brackets. But I am unsure where to insert that code, in my models.py or views.py. I have looked into it and am confused, I am using the import re, but have seen that Django has their own import RegularExpressionVerificationError and do not know which one is better to use.
Kind Regards,
Welcome @sreeves0 !
Don’t worry about the (relatively) minor details about posting here - as long as you adhere to the Code of Conduct in your posts and make a reasonable attempt at following any advice provided by the moderators, you won’t have any problems.
Now to get to the core of your post, you wrote:
Depending upon how you want to handle this data, you actually have three places where the code could go. In addition to the models.py and views.py, it could possibly also go in the forms.py. And it’s really up to you as to where you want to put it.
If you haven’t already worked your way through the Official Django Tutorial, I suggest you do so. If you have, then I suggest you review the work you’ve done. In either case, what you want to focus on at this point is the sequence in which things occur. You’ll want to try and understand how Django goes through the cycle from accepting a request, invoking a view, and (possibly) interacting with forms and models before rendering a response.
Once you can logically follow a request from it being submitted until the response is returned, you’ll see that the code you’re looking to add could fit in a number of different places, and where you choose to use it may to some degree depend upon whether you only need to execute this code in one place or in many places throughout your project - and even that can change over the life span of this project.
1 Like
This is just a side question about the GenericIPAddress field, but I am having trouble finding resources for this specific question. In my app I can already insert an IP address and such in a CSV file or textinput, but I am having trouble adding an IP address with CIDR notation. is there something I can insert to allow CIDR? Or will I have to use a different field for this purpose, which I would like to avoid.
This is how it looks in my models.py:
models.GenericIPAddressField(blank=False, verbose_name=“IP Address”)
Not only would you would need to use a different field, but additionally, you’d need to create it yourself.
The IP Address field in Django is a very thin wrapper around the Python ipaddress library. While that library does support both an ip_address
and an ip_network
object, they are different objects, and I only see support for ip_address
in Django.
1 Like