Correct pattern to call external api during form saving
Hi My requirement is 1)User adds postcode to form 2)I call external api with postcode to get details of the postcode,local authority,MP etc 3) save this data into another model My question is what is the correct pattern for doing this? Where should I call the api? Should this be done before the user form is saved(is_valid)? What if the api goes down? How to handle that gracefully? Thank you Chris -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACQBJYWv7Oa-h-JHXXDKKitHv-p%3DdL%2B--eBY0kux5MqLoKnuJQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: {% csrf_token %}
Hi Alex Try this: return render_to_string('home.html', request=request) cheers Chris On Tuesday, 26 September 2017 18:29:15 UTC+1, Alex Kleider wrote: > > I'm using test driven development (going through Harry J.W. Percival's > book) and have found that the following code fails because the template tag > ( {% csrf_token %} ) is rendered by the home_page view function but not by > the django.template.loader.render_to_string function (and so the > assertEqual test fails.) > > ...templates/home.html: > ... > > id="id_new_entity" > place_holder="Pick a name for your new entity." /> > {% csrf_token %} > > ... > > Testing code: > > def test_home_page_returns_correct_html(self): > request = HttpRequest() > response = home_page(request) > returned_html = response.content.decode() > expected_html = render_to_string('home.html') > self.assertEqual(returned_html , expected_html) > > returned_html and expected_html are the same except that returned_html > contains the following line (and the other doesn't:) > value='Ev0j62rUtwdpOwjS5FN7B1VT38hE75W0JVZUJQy8IpcvzTH0MCexoRSpQvofoDoW' /> > Infact, expected_html doesn't even contain the > {% csrf_token %} > line. > > Can anyone suggest a work around? > Thanks in advance. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4ff3ff89-f6ac-4e82-8d54-964be94e383c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Referencing objects that don't exist that will be created in forms
Hi I have the following scenario in my forms.py: UNIVERSITY_CHOICES = University.objects.values_list('id', 'name') class UniversityForm(forms.Form): name = forms.CharField(widget=forms.Select(attrs={"class": "selectpicker", "data-live-search": "true","title": "find university..."},choices=UNIVERSITY_CHOICES), required=False) The migration fails as it is expecting the university table to exist but does not yet. This is clearly an anti-pattern on my part. What is the correct way to approach this? thanks Chris -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACQBJYWMPHHnH7fs4ogYerxoT953pyX_K9eeKjgoMjjefXwo8w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: Referencing objects that don't exist that will be created in forms
Hi All Ive overcome it by adding doing this: def __init__(self, *args, **kwargs): super(UniversityForm, self).__init__(*args, **kwargs) self.fields['name'].choices = University.objects.values_list('id', 'name') This makes sense. Don't reference objects that will be parsed before they exist On Thursday, 19 October 2017 10:23:36 UTC+1, Chris Wedgwood wrote: > > Hi > > I have the following scenario in my forms.py: > > UNIVERSITY_CHOICES = University.objects.values_list('id', 'name') > > class UniversityForm(forms.Form): > name = forms.CharField(widget=forms.Select(attrs={"class": > "selectpicker", "data-live-search": "true","title": "find > university..."},choices=UNIVERSITY_CHOICES), required=False) > > The migration fails as it is expecting the university table to exist > but does not yet. > This is clearly an anti-pattern on my part. What is the correct way to > approach this? > > thanks > Chris > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d5f6e5d8-d03c-4c80-8fcb-fdc061cfc2e3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re-runnable sql scripts in the migrations framework
Hi As part of my deployment process I want to be able to have some db scripts that are re-runnable For example I have some internal configuration tables that I would like to be able to change values with a merge statement in the same file but not have to create a new migration file I suppose my question is does the migration framework allow for re-runnables? If not can anyone recommend a good approach for this? thanks Chris -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACQBJYU%3D0yGyxGXEL1VZ0vBU14HkgzQiF7sNzavYaTtUDTzJgw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Adding stored procedures
Hi All I am using SQL SERVER in my latest django project and I am going to be using some stored procedures. My question is about how to go about deploying stored procedure changes with django. I have looked through the migrations documentation which looks very specific to model changes. Has anyone had experience of having to create other things such a stored procedures/views/functions? thanks Chris -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACQBJYU5CDhCrP7cNRpzp7fRDr1pyf48css-EaVW%3Drj65SmEsg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: Adding stored procedures
Thanks Matthew I probably need to think about this some more I think using runsql will work. Do you know if you can set migrations to be rerunnable? It would be useful to be able to change something like a stored procedure and then it gets dropped and recreated each deployment Saying that the stored procedure is going to be used for an import task that isn't actually related to Django so probably needs to be deployed by another mechanism and I should leave migrations for only DJango specific changes thanks Chris On Wednesday, 18 April 2018 07:50:52 UTC+1, Chris Wedgwood wrote: > > Hi All > > I am using SQL SERVER in my latest django project and I am going to be > using some stored procedures. > > My question is about how to go about deploying stored procedure changes > with django. I have looked through the migrations documentation which looks > very specific to model changes. > > Has anyone had experience of having to create other things such a stored > procedures/views/functions? > > thanks > Chris > > > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20a5ed5a-97f4-413b-8700-58606b781a2f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: How to best secure environment variables (secret key, passwords etc.) stored in .yml files?
Hi Tom You are definitely not overthinking this. it's important. This is an area that has baked my noodle for a while now and I always am left wondering "Do I have this right?" "Am I vulnerable to attack?" . and I still haven't figured it out completely. It's like static files I never really feeel like I get it entirely :) Firstly you should never need to store a password/token/secret in Source Control ever. If you are stop and think there must be a better way. I use environment variables .env to store my secrets but the trick is ALWAYS put that in your .gitignore file. If you start a new git repository there is an option to create a .gitignore file for Python that is a great starting point. To complement my *.env* file it has a .env.example file that I DO put in source control with a dummy password. .env file: MAILGUN_API_KEY =asjdhasds78dy9s8dy012287e210eu209e72 .env.example: MAILGUN_API_KEY=ThisIsNotARealToken So when I do local development I can populate my .env fie with local dev secrets. For production deployments, I use *Ansible *for which I provide production tokens and secrets in a separate file also not in source control. The Ansible deployment requires an ssh password that I store in a Password Manager that has two-factor authentication. The docker-compose file can read environment variables from the .env file. Have a look at Django-Cookiecutter and see how they do it. That helped me a lot when I started out cheers Chris On Thursday, 30 January 2020 12:41:01 UTC, Tom Moore wrote: > > Hi there, I'm following the guidelines by making sure the environment > variables are stored outside of the settings.py files. > > The project is "dockerised" and so the environment variables have been > stored in files *docker-compose.yml* and *docker-compose-prod.yml*. > > This includes things like the project's secret key, API keys, and database > passwords. > > *My question is: * > • Just because environment variables are stored in .yml files, won't they > be equally insecure the moment I commit the project folder to a git repo > (and especially if I push that repo to GitHub)? > e.g. the Secret Key will forevermore be stored in the git repo (in earlier > versions, even if I later move it to another file in subsequent commits). > > Is there an even more secure way of storing environment variables? Or am I > overthinking it (as I'm the only developer and the GitHub repo is set to > Private)? > > Many thanks in advance for your help. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55f28dec-7c9a-4cae-b658-f89772aa1bd7%40googlegroups.com.
Docker performance on Mac OS X
Hi All I generally use the Django Cookiecutter project to start my projects and have started using *docker* for my local development. However I have started to get some really awful performance with pageloads taking 30-40 seconds. I am know considering stopping using docker entirely. Has anyone else encountered this on Mac OS and have any wisdom to share to alleviate? Thanks Chris -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACQBJYXuQ7i3oe22y%2B9c9dOVb5%2Bdc-YjyyrkUZ_GZkbCEp1zMw%40mail.gmail.com.
Re: Docker performance on Mac OS X
Hi Jason Thank you for your response. I misdaignosed my issue. It actually had nothing to do with Docker and instead was a slow performing form in the django admin that had a widget referencing a foreign key which was taking forever to build. I fixed it with raw_id_fields, so am now back on docker In terms of my docker usage I will verse myself in allocations etc cheers Chris On Monday, 23 September 2019 12:18:03 UTC+1, Jason wrote: > > What kind of hardware are you working with? What resources do you have > allocated to Docker. Are you including tens of thousands of files in > volume mounts? > > We use docker extensively at work, and I've found it helps to have at > least half of your machine's hardware available to docker. My 2018 MBP has > 5 cores and 10GB RAM allocated. > > However, docker is very sensitive to the amount of files being included > and being watched over. SO if you have thousands to tens of thousands of > files in your volume mount symlinks, performance is going to be poor. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f9a2c6a3-9199-4eee-b362-024f38ba9469%40googlegroups.com.