|
I've been troubleshooting getting forms to display cleanly in Django. The way I learned to display forms in the template from the Polls tutorial looks like this:code:
Unable to get it formatted cleanly, I found online that using '{{ form.as_p }}' will show anything with 'label' in the form class as a label tag, which you can then manipulate with CSS. The method I used with help_text showed un-tagged text above the <input> tags in source view. The official docs on forms mention both methods. Thoughts? Also looking for advice on getting the labels/help_text to align left, and the fields to align right. I've tried this: code:
Dominoes fucked around with this message at 10:20 on May 26, 2014 |
# ¿ May 26, 2014 09:58 |
|
|
# ¿ May 16, 2024 05:07 |
|
Lumpy posted:
Here's an MS-paint mockup of what I'd like:
|
# ¿ May 26, 2014 19:40 |
|
supermikhail posted:Sorry for hijacking, but is this applicable to your problem: http://stackoverflow.com/questions/2820586/how-can-i-control-the-width-of-a-label-tag? CSS code:
Django code:
Dominoes fucked around with this message at 20:09 on May 26, 2014 |
# ¿ May 26, 2014 20:04 |
|
Thanks. Any words on how to make a login page redirect to page that triggered it? This Stack Overflow post addresses it, but the solution only works if logging in from a link; I'm using the @login_required method, with no link to reference. I've tried this: Python code:
I feel like this is a very common problem - most major websites I use that have logins don't address this - and it's really annoying! Ie you navigate a few pages deep in a website, are met with a login wall, then are spit out at the top after logging in. Dominoes fucked around with this message at 08:20 on May 28, 2014 |
# ¿ May 28, 2014 08:03 |
|
supermikhail posted:One (rather complicated) way I can think of is have the login button be part of a form with a hidden field for the current url (found like this http://stackoverflow.com/questions/2882490/get-the-current-url-within-a-django-template). This goes to your GET login view which passes the hidden field on to the login page, from which you POST it back to the login view, and voila. But maybe I'm overthinking it. Nimrod posted:request.META['HTTP_REFERER'] ? Thermopyle posted:FWIW, I use django-allauth on all my projects requiring registration and login stuff, and when you get redirect to a login page it appends the url to redirect to on login to the url. ufarn posted:Here is what project of mine looks like: Thank you very much. I used Nimrod's solution of request.META['HTTP_REFERER']: Python code:
Dominoes fucked around with this message at 12:35 on May 31, 2014 |
# ¿ May 31, 2014 12:32 |
|
I'm getting duplicates in queries involving many-to-one relationships. One airfield can have many runways. The query below is returning the Airfield object for each runway that meets the length criteria. Ie if I have an Airfield that has three Runways greater than min_rwy_len, the airfield is in the result 3 times. Python code:
edit: running distinct() at the end of the query fixes this. Dominoes fucked around with this message at 06:33 on Sep 8, 2014 |
# ¿ Sep 8, 2014 06:04 |
|
I have a basic form view. It's redirecting to the top-level url, (For the test server, http://127.0.0.1:8000/) and therefor returning a Page Not Found error instead of staying on the same url and displaying the result, as it should.Python code:
|
# ¿ Sep 9, 2014 23:11 |
|
Thanks - good call. The problem was in the action field of my HTML template.
Dominoes fucked around with this message at 23:37 on Sep 9, 2014 |
# ¿ Sep 9, 2014 23:23 |
|
Is there a way to query for anything, like a wildcard? Improvised method below, looking for something more explicit.Python code:
Dominoes fucked around with this message at 13:25 on Sep 12, 2014 |
# ¿ Sep 12, 2014 13:23 |
|
I want to be able to allow any surface, if paved_only is False.
|
# ¿ Sep 13, 2014 04:44 |
|
surface might have a value. Repeating the filter statement for each if/or doesn't make sense if there are multiple filter fields; ie the solution I posted, while convoluted, is cleaner. I'm looking for something like query_parameter=anything
|
# ¿ Sep 15, 2014 18:51 |
|
fletcher posted:I want to store a latitude & longitude in a Django model and display the points on a Google Maps widget. My database is PostgreSQL. Should I just use floats? django-geoposition? GeoDjango? Dominoes fucked around with this message at 12:01 on Oct 29, 2014 |
# ¿ Oct 29, 2014 11:33 |
|
I'm setting up a simple database of books, and am looking for advice on database design. I'm leaning towards having a separate entry with a unique ISBN, with a field that links each entry to other entries that are of the same work. How would I set up this relationship? It's somewhat like a ManyToMany, but with items of the same table. An alternative setup would be to have one entry for each work, with different ISBNs for each edition or w/e causes works to be fragmented. Or two tables, one with Works and a second for ISBNs, and a ManyToOne relationship of ISBNs to Works. How would y'all set this up, and if I went with my first option, how do you actually code a ManyToMany with the same table?
|
# ¿ Oct 9, 2015 15:24 |
|
Mata posted:If each work has one or more ISBNs, I would suggest dividing them up between two tables, to minimize data redundancy. If you want to go with the first option, you still need a second table to model the relationships within the first table. I did a similar thing where I want users to have relationships with other users (e.g. you can block users or add them as friends) for which I created a "Relationship" table where each row describes the relationship between two users: Here's what the two-table approach I described looks like: Python code:
Dominoes fucked around with this message at 15:47 on Oct 9, 2015 |
# ¿ Oct 9, 2015 15:42 |
|
Checkout enumfields I'm looking for advice on querying a Django Postgres database based on string similarity. One approach would be to query all objects in the database, then use the builtin difflib library on the results, but that would be slower than a natural query. Is there any way to do this using a query? Dominoes fucked around with this message at 13:30 on Oct 22, 2015 |
# ¿ Oct 20, 2015 01:33 |
|
fletcher posted:Probably have to use some raw sql to get to the more advanced functions, I would imagine: http://www.postgresql.org/docs/9.5/static/fuzzystrmatch.html For model fields, what's the difference between help_text and label?
|
# ¿ Oct 31, 2015 16:52 |
|
porksmash posted:Label is the name of the field, help text is generally more verbose on how to use the field. Like for a create password field, the label would be 'New Password' and the help text might be '8 characters or more, requires at least one number and special character'
|
# ¿ Nov 2, 2015 20:31 |
|
I'm looking for help with forms. It's easy to find instructions on making HTML/js forms, and using Bootstrap etc to make them look nice. I'm having trouble integrating this with Django . I'm using Crispy forms, and am trying to make a search bar with a clickable submit button with a mag-glass icon. Here's the code I'm using to make what I describe, but without the button being clickable; submit works only with enter button:Python code:
Dominoes fucked around with this message at 21:48 on Dec 1, 2015 |
# ¿ Dec 1, 2015 21:46 |
|
Thanks. Do you know if there's a way to make FieldWithButtons work with the icon (seems to only work with text), and placeholder text? The documentation only references it via one example, and with only the button text and name.
|
# ¿ Dec 2, 2015 10:24 |
|
I'm curious too. I've been making a file called private.py that I put on gitignore. I use an if/else statement based on a server environment var in settings.py to prevent an import error when deployed. It seems like an awkward solution.
|
# ¿ Dec 7, 2015 19:52 |
|
Connecting to a Heroku database locally. Or storing API keys.
|
# ¿ Dec 7, 2015 21:37 |
|
Does anyone know how to make date / time / datetime widgets work in Django forms? I've found nothing but broken addons and ancient admin hacks.
|
# ¿ Apr 16, 2016 15:10 |
|
Hey dudes: How do I access files (like a pdf or w/e) from code in a django app on Heroku? I've been pulling my hair out regarding this!! -Works locally -When I clone, the file's there, so it is getting pushed to Heroku -Static files like Javascript and css work fine; dj-static configured. I've tried dumping the file I need the code to access everywhere; main proj and app static, staticfiles directories, the main proj directory, the main app directory. Nothing, and no leads on troubleshooting since it's only reproducible when deployed. You should see my commit log! I get a standard FileNotFound Error. Based on my weak understanding of serving static files, this is one one in the trad sense; it's accessed by the code, not the user. edit: Got it working, after dropping the file in about ~10 locations simultaneously; no idea which one was correct! Dominoes fucked around with this message at 11:50 on Feb 6, 2017 |
# ¿ Feb 6, 2017 11:00 |
|
Hey dudes. Looking to send data from javascript to Django, concluding with a page refresh. I've been sending data like this:JavaScript code:
edit: Looks like I can get a better result by modifying elements with Jquery to look like how they would after a page refresh; should be more responsive, although with more/clumsier code. Dominoes fucked around with this message at 00:42 on Feb 25, 2017 |
# ¿ Feb 25, 2017 00:36 |
|
Data Graham posted:Add a success: callback function in your ajax call, and do a document.location.reload() in it.
|
# ¿ Feb 26, 2017 12:02 |
|
-
Dominoes fucked around with this message at 21:56 on Mar 1, 2017 |
# ¿ Mar 1, 2017 21:42 |
|
Thermopyle posted:edit: this post made more sense in relation to the previous post that was removed! I'll leave it as commiseration. -Use migrations until they break. Adding tables and fields is usually fine; modifications are a gamble. -If there's no real data in the db, ie it's early in the dev process, wipe the DB and migrations; start fresh. Always fixes it. -If there's real data saved, update the DB with SQL from a console for the app that's broken (Ie other apps might still work with migrations). Migrations will never work again for that app, but your website and database will work. Is there a way to unbreak migrations, once they're broken? Dominoes fucked around with this message at 18:08 on Mar 2, 2017 |
# ¿ Mar 2, 2017 17:39 |
|
And then you get errors because django can't find so and so field because DUH YOU DELETED IT! Root cause is probably I'm not making proper migration files, and relying too much on makemigrations. I feel like this is a problem that could be solved by Django telling you "Whoah I'm not sure what you're doing, please make a migration file", rather than irreversibly* breaking. * Irreversibly meaning like when a car's totaled; not that it's broken beyond repair, but when it's easier to back everything up and start fresh, or do everything in SQL, than troubleshoot. I feel like I'm not out of line saying this is an unusually counterintuitive process for Python. Tell me if I'm off-base here: makemigrations is how you change your database, after editing, adding, or removing model fields. Unless your modification is unsuitable for makemigrations... And there's no official guidance about what unsuitable means, and django doesn't warn you, or provide an undo option if it doesn't work. So yes, I expect them to break. And I expect there's room for improvement, ie this will be fixed within a few years. Dominoes fucked around with this message at 23:14 on Mar 2, 2017 |
# ¿ Mar 2, 2017 22:59 |
|
Hey dudes. Do y'all use Django's forms, or make your own in HTML/JS? Django recommends you use their forms when able due to validation etc, but it seems like if you're doing something other than a very basic form that matches a model directly, they're a PITA. Ie: Want a date/time picker widget? Dynamic behaviour? A customisable layout? Doable, but a pain. Dominoes fucked around with this message at 18:11 on Apr 28, 2017 |
# ¿ Apr 28, 2017 17:46 |
|
That sounds familiar. It's been a different reason that drives it each time. For example, today I switched from a Django model form to an HTML form submitted via AJAX since I wanted date/time widgets, a checkbox that automatically sets the time to a certain thing and disables the inputs, and customized dropdowns. Another case was having items be read-only on the page, but have a button that turns them into dropdowns and input fields in an 'edit' mode. I suppose this all depends on the use case. These things could be handled by Django, but AFAIK, the API's not there. (It wouldn't surprise me if we get elegant input-widget handling in a future release; it seems so obvious.)
|
# ¿ Apr 28, 2017 18:17 |
|
Hey dudes, I'm looking for info on what Django Rest framework is used for. Context: I was reading about using Django and React together (AFAICT it's straightforward; just put a <div> in your template that ReactDOM's render func points to), and almost all results pointed to Django Rest Framework. From skimming their site, it seems like it's supposed to clean up messes when passing AJAX data to/from the front end? ie avoid awkward deserializing code like this? Python code:
|
# ¿ May 1, 2017 00:56 |
|
I'm looking for wisdom on encrypting DB fields. I stumbled acrossthis module called Django extensions, which offers an EncryptedCharField doing what I'd like; however, it requires on a package called KeyCzar, which no longer works. Does anyone know how to handle encrypting DB fields with Django?
|
# ¿ Jun 13, 2017 16:58 |
|
I'm working on a scheduling webapp for a government agency as part of a quasi-official function at work. I'd just like to encrypt first/last and organization names. I'm forcing an HTTPS connection, and it seems like the Heroku/AmazonAWS database setup is fairly secure. This is unknown territory, and is a gray area. ie software like this is traditionally hosted locally or semi-locally, takes years to make, costs millions of dollars, sucks, and the devs don't know what users want. The info's not particularly sensitive, but the names are considered PII, especially when associated with the org. Do you have any recommendations, encryption or otherwise?
Dominoes fucked around with this message at 20:51 on Jun 13, 2017 |
# ¿ Jun 13, 2017 20:37 |
|
Thermopyle posted:Its of note, that on production tiers Heroku/Amazon already encrypts the data volumes including your database...so what you're wanting to do with a single field is basically already done for your whole db. A compromise would have to be specific enough to catch the data on the fly or getting access to Amazon's key storage.
|
# ¿ Jun 13, 2017 22:06 |
|
Is there a template system that works for django that lets you wright full-up python in the Templates? I never liked how restricted Django templates are: simple things work, but anything else (calling functions, methods etc) requires setting up intermediate data structures or custom template tags in the Python code. React's JSX provides an alternative where you write full-up JS with HTML tags; it has none of the template system's limitations. For pages that don't require interaction, it'd be nice to have something as powerful as JSX but in Python; is this possible?
Dominoes fucked around with this message at 20:55 on Aug 12, 2017 |
# ¿ Aug 12, 2017 20:47 |
|
Thanks. It feels awkward for me; I made many attempts to get used to this style, but never succeeded. I don't like bouncing back and forth between python code and templates when setting up how my page should look. Ie I may have something written in my template, but want to change something later that requires a function call; have to rewrite the whole thing in Python, then find a way to wrap it in a template-friendly dict etc. Or guess where I put the logic when looking at older code. I also don't like creating throw-away data structures for templates; ie I may have a consistent data structure or way of passing around info that's consistent throughout my program, that is incompatible with rigid templates. I suspect this is similar to the front-end philosophy difference between React's JSX and Angular's templates; ie some people can't stand how JSX mixes program logic with UI. Dominoes fucked around with this message at 22:48 on Aug 12, 2017 |
# ¿ Aug 12, 2017 22:41 |
|
Thermopyle posted:Split code into these modules by thinking in commands. Data Graham posted:Something that I've only fairly recently come to make friends with is the notion of putting your derived data into @property methods in your models.
|
# ¿ Aug 13, 2017 22:58 |
|
Thermopyle posted:I mean, if, as part of your view, you have code that does x, y, and z, how are you going to use that code elsewhere? What if you want a view later that wants x, y, and z along with other stuff?
|
# ¿ Aug 14, 2017 01:31 |
|
Hey dudes. Looking for wisdom on what URL to query for Django Rest tied to a Create-React-App setup. Setup: standard, unejected CRA folder inside Django folder. Django's static and temple settings point to frontend/build. Dummy package.json in main folder that tells Heroku which node version and packages to install, then points to the real one in frontend, which commands a build on install. Can use locally from http://localhost:8000 for Django; eg using the built frontend, admin page etc, or http://localhost:3000 for the hot-loading CRA server. How I'm making the API calls: JavaScript code:
JavaScript code:
edit: Trying something like this: JavaScript code:
Dominoes fucked around with this message at 13:13 on Apr 14, 2018 |
# ¿ Apr 14, 2018 12:33 |
|
|
# ¿ May 16, 2024 05:07 |
|
Hey dudes. Looking for wisdom on creating a non-saved subclass in a model. Eg I don't want to save a separate entry; just using it to reduce code.Python code:
Eg without the DB tie-in: Python code:
Dominoes fucked around with this message at 07:45 on Aug 3, 2018 |
# ¿ Aug 3, 2018 07:35 |