Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Trouble updating Div with JSON data
Follow on from my last question, I have a django web app which I am trying to use to display data on a HTML page with data from a database using the JsonResponse method. The website allows users to enter the details of a new product. I am trying to design it so that when they enter the details, a Div underneath will automatically update and show the new item that has been entered as well as the existing items in the database. Here is my code: The index page: <!DOCTYPE html> <html> <body> <div> <h2 id="title">Create product</h2> <input id="name">Name</input> <br> <input id="description">Description</input> <br> <input id="price">Price</input> <br> <button id="add-product">ADD PRODUCT</button> </div> <div id="productList"> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> document.getElementById('add-product').onclick = function(){ sendData(); getData(); } function sendData(){ var order = { name: document.getElementById('name').value, description: document.getElementById('description').value, price: document.getElementById('price').value }; $.ajax({ type: "POST", url: 'create/product', data: order, success: function(newProduct){ console.log("success"), $('#name').val(""), $('#description').val(""), $('#price').val("") } }); }; function getData(){ $.ajax({ url: 'view/product', dataType: 'json', type: 'GET', success: function(data){ $.each(data.prod, function(index, element){ $('body').append($('#productList', { text: element.name })); }); } }); } </script> </html> The views.py file: from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt from products.models import Product from django.http import HttpResponse, JsonResponse def … -
Having trouble provisioning SendGrid for Heroku
I am trying to use SendGrid to send emails from my application. I set up an account with them, followed all the instructions, and got it working for my development environment on my local server. It sends emails and everything is fine...until I try to do it for production on Heroku. I tried it without provisioning the add-on, and I get a 'connection unexpectedly dropped' error. The debug says it was invalid authentication, but my username/password are correct. They are the same ones I use on the dev server where it works. So then I try to provision the add-on, thinking that maybe Heroku forces apps to use the add-on, and I get a 'string index out of range' error. This is when trying to send a password reset email through Django's default package. Anyone know what might be going wrong? Thanks in advance for any help. I have the following config variables set in Heroku: EMAIL_HOST = smtp.sendgrid.net EMAIL_PORT = 587 EMAIL_USE_TLS = True SENDGRID_API_KEY = my sendgrid api key SENDGRID_USERNAME = my sendgrid username SENDGRID_PASSWORD = my sendgrid password I'd rather do it without the add-on if possible since I've already made that account and configured DNS settings … -
GrapesJS tag with "src" requests
I implemented a small GrapesJS editor that allows to edit Django/Jinja2 templates. GrapesJS by default is trying to get the image preview of my element: <a href="{{product_url}}" target="_blank"><img src="{{image_url}}" width="130"/></a> The requests happens everytime I load the template into the GrapesJS editor: http://localhost:8000/%7B%7Bimage_url%7D%7D 404 (Not Found) GrapesJS editor though works fine even with this 404 call. But I would like to do handle the request to the image. So instead of GrapesJS making a default call to http://localhost:8000/%7B%7Bimage_url%7D%7D I would like to render a placeholder image let's say: https://placekitten.com/200/300 It occurred to me that this can be handled on an Event: https://github.com/artf/grapesjs/wiki/Assets#events but that doesn't seem to be what I'm after. -
Django custom auth model registration form returns unique username error
I have my own user model called User. I have a user registration form that returns this error below: duplicate key value violates unique constraint "users_user_username_06e46fe6_uniq" DETAIL: Key (username)=() already exists. Could it be that the user model is getting saved twice? This is my form: class FighterSignUpForm(UserCreationForm): class Meta(UserCreationForm.Meta): model = User fields = ('first_name', 'last_name','email','password1', 'password2',) @transaction.atomic def save(self): user = super().save(commit=False) user.is_fighter = True user.is_active = False user.save() return user This is my view: class FighterSignUpView(CreateView): model = User form_class = FighterSignUpForm template_name = 'registration/user_signup.html' def get_context_data(self, **kwargs): kwargs['user_type'] = 'example' return super().get_context_data(**kwargs) def form_valid(self, form): user = form.save() This is the custom user model: class UserManager(BaseUserManager): use_in_migrations = True def _create_user(self, email, password, **extra_fields): if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=None, **extra_fields): extra_fields.setdefault('is_staff', False) extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_staff') is not True: raise ValueError('Superuser must have is_staff=True.') if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(email, password, **extra_fields) class User(AbstractUser): email = models.EmailField(_('Email'), unique=True) USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] is_type1 = models.BooleanField(default=False, … -
Problem with overwrite save() for Model with ManyToMany field
I have a problem with overwriting save() for my Model. I want my Pizza Model to calculate amount of chosen toppings (from PizzaTopping Model) and - with this information - get price from PizzaType Model field. When i submit form, Pizza Model print me info, that there is no counted toppings and set the price for "0-topping" price. In admin panel i can see, that Pizza Object was saved successfully, even with chosen Toppings, but with wrong calculated price. When i change toppings, price change to good value, but with "1-saving delay" (when i save, nothing happen, but next time i save - it works). models.py class PizzaTopping(models.Model): name = models.CharField(max_length=60, unique=True) def __str__(self): return self.name class PizzaType(models.Model): name = models.CharField(max_length=60, unique=True) price_sm_0 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_sm_1 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_sm_2 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_sm_3 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_sm_4 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_lg_0 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_lg_1 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_lg_2 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_lg_3 = models.DecimalField(max_digits=4, decimal_places=2, null=True) price_lg_4 = models.DecimalField(max_digits=4, decimal_places=2, null=True) def __str__(self): return self.name class Pizza(models.Model): pizza_size = models.CharField( max_length=2, choices=( ("sm", "small"), ("lg", "large") ), ) type = models.ForeignKey(PizzaType, on_delete=models.CASCADE, null=True) toppings = models.ManyToManyField(PizzaTopping, blank=True) price = … -
Fill a graphic pdf/doc with data from a model in Django admin
I have a pdf document that I would like the ability to fill out in the appropriate places from data from a model in the Django admin by clicking on a custom button in the change_form.html. Is this at all possible? I couldn't find any documentation (at least for the admin) on how to even approach this anywhere. Thanks so much, Dev -
Access to model field in ModelForm
I have blog and I am able to comment my post. But in the view I have to check if author of comment is the same as logged user. For Blog model - this is simple - I need only self.get_object() But there is a problem for CommentForm in detail view. How can I get author field of comment model in BlogDetailView? class BlogDetailView(generic.DetailView, FormMixin): template_name = 'blog_detail.html' context_object_name = 'blog_detail' model = Blog form_class = CommentForm -
How to get django-ldapdb to recognize datasource as LDAP
I have followed the directions in the django-ldap README and I cannot seem to get django-ldapdb to act like it's making an LDAP query. The following has been edited on a brand new instance of Django v.2.1.2 using with Python 3.7: Changes to settings.py: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), }, 'ldap': { 'ENGINE': 'ldapdb.backends.ldap', 'NAME': 'ldaps://my.server', 'USER': 'cn=some user', 'PASSWORD': 'somePassword', } } DATABASE_ROUTERS = ['ldapdb.router.Router'] New models.py: class MyPerson(ldapdb.models.Model): base_dn = "ou=people,dc=ucsf,dc=edu" object_classes = ['person', 'myPerson] uid = fields.IntegerField(db_column='uid') displayname = fields.CharField(db_column='displayname') eid = fields.CharField(db_column='eid') def __str__(self): return self.uid def _unicode__(self): return self.uid The query in my view. First I tried: result = MyPerson.objects.filter(uid=99894) Then I tried: result = MyPerson.objects.using('ldap').filter(uid=99894) Running the Django dev server in PyCharm's debugger I can see that result receives a QuerySet with a message of: Unable to get repr for <class 'django.db.models.query.QuerySet'> What do I mean by "message". Honestly I'm not sure, but the debugger shows this: Also, it seems that though the db member of the QuerySet is 'ldap' the query member shows an SQL query, not an LDAP filter. As I traced the HTTP request through the URL routing, to the view, to the query, and then … -
How to fix django machina attachment cache backend not configured (but it is)
Hi I installed django-machina, a forum app, on my local development server. I customized it to work the way I want and it works perfectly. In my settings.py is the following as required. Note that 2 caches are used. The first for the site, and the 2nd for the forum which requires FileBasedCache. I made the /tmp/django_machina_forum directory too. So it exists. CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', }, 'machina_attachments': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/tmp/django_machina_forum', }, } When I try to put this on the vps for the same project, after going through the documentation again, installing the proper settings.py, pip install django-machina and all it's requirements, I'm getting this error after I python manage.py makemigrations: django.core.exceptions.ImproperlyConfigured: The attachment cache backend (machina_attachments) is not configured As you can see, 'machina_attachments' is properly configured. To try and debug this, I went to machina.conf.settings and machina.apps.forum_conversation.forum_attachments.cache to make sure their imports weren't messed up. In the cache.py file we have: def get_backend(self): try: cache = caches[machina_settings.ATTACHMENT_CACHE_NAME] except InvalidCacheBackendError: raise ImproperlyConfigured( 'The attachment cache backend ({}) is not configured'.format( machina_settings.ATTACHMENT_CACHE_NAME)) return cache So I go to the settings file they imported here, and I find ATTACHMENT_CACHE_NAME = getattr(settings, 'MACHINA_ATTACHMENT_CACHE_NAME', 'machina_attachments'). … -
Error in Django Get request that dosen't make sense
I have a basic Django app right now which allows users to add items to a database. When the product is added, the list of items should update when a new item is added via the form, and display that Product and all the other Products already in the database. Here is the code I have so far: This is the views.py file with my current implementation of the getData() method at the bottom: from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt from products.models import Product from django.http import HttpResponse, JsonResponse def index(request): return render(request, 'index.html') @csrf_exempt def createProduct(request): if request.method == 'POST': name = request.POST.get('name') description = request.POST.get('description') price = request.POST.get('price') newProduct = Product( name = name, description = description, price = price ) newProduct.save() return HttpResponse('') def viewProduct(request): if request.method == 'GET': ProductList = Product.objects.all() products=[] for Product in ProductList: products.append({"name": Product.name, "description": Product.description, "price": Product.price}) return JsonResponse(products) The index.html page: <!DOCTYPE html> <html> <body> <div> <h2 id="title">Create product</h2> <input id="name">Name</input> <br> <input id="description">Description</input> <br> <input id="price">Price</input> <br> <button id="add-product">ADD PRODUCT</button> </div> <div id="productList"> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> document.getElementById('add-product').onclick = function(){ sendData(); getData(); } function sendData(){ var order = { name: document.getElementById('name').value, description: document.getElementById('description').value, price: document.getElementById('price').value … -
How to reset_sequence to default id field sequence in DjangoModelFactory
With a factory like: class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User The model's id field will by default start at 1. I want the id to start at a higher number, eg 50000. Using UserFactory.reset_sequence(50000) does not work unless you add this line in the factory id = factory.Sequence(lambda n: n) I have a lot of tests that depend on the default id increment behavior so this way is a no go. -
Django QuerySet date manipulation
I have a model storing, among other things, a timezone datetime object. I am reaching out with an Ajax call from the website to one of my views, which does the following call: def uploads(request): user = User.objects.get(username=request.user.get_username()) cases = Case.objects.filter(user_id=user.pk).order_by('-uploaded_on')[:5] return JsonResponse(serializers.serialize('json', cases, fields=('col1', 'col2', 'col3', 'uploaded_on')), safe=False) When a JSON response is received at the Front End, it gets parsed with JS and analyzed. What I would like to do here, is to modify the 'uploaded_on' column with user's timezone (which I can already access with user.timezone, currently displayed as a string e.g. 'Germany/Berlin', but can easily convert it to a necessary object with e.g. pytz package). When I try to iterate through the QuerySet like below, the values received on the Front End do not change: for case in cases: case.uploaded_on.astimezone(pytz.timezone(user.timezone)) This is probably related to the fact that QuerySets are lazy, like described in Django's documentation. Can anyone suggest how this can be done? A bonus question as well: with Django's serializer, can I pass along more than one QuerySet in a single JSON response? When I try to pack a few in a list, I get a MultiValueDictKeyError: return JsonResponse(serializers.serialize('json', [cases, cases2]), safe=False) -
Queryset in kwargs vs jquery - better for AJAX?
I'm building a dice rolling app for fun and (much more importantly) for learning Django / improving my programming skills. I've got the app nearly ready for alpha deployment (it's still on localhost), except for adding AJAX, which I had been told should be relatively easy to add at the end. I am currently passing to the html template three different querysets in the kwargs. But now I wonder if my decision to pass the querysets in as kwargs (instead of, say, writing a JQuery) was the right one. If the app is to be functional, users will need to be able to see the three querysets as they are updated (or shortly thereafter). The easiest thing to do would be to have AJAX refresh the page every n seconds. But I'm not certain that would be the optimal solution. There are two different input fields where a user could conceivably be entering longer &/or complicated inputs that I don't want to have disappear when the AJAX refreshes. I freely admit that I am just learning AJAX & have no real experience with XML. So, the questions: How can I best use AJAX to update the querysets on the page … -
Django - update_index updates both test and production sites
This is a weird one that I cannot figure out. I have two databases - test and production. I have two websites - test and production. Django is using elasticsearch for the indexing. They are running off of different servers, have different IP address, etc. There is no link at all between the two databases. They are maintained independently. However, when I run update_index or rebuild_index on either the production or test site, the indexes on BOTH sites are getting updates. The two databases still remain independent and the data there is not transferred at all. It doesn't matter if I run the command on test or on production - both will be updated from either source. This is obviously causing me many problems and I cannot for the life of me figure it out. TEST DATABASE AND HAYSTACK CONNECTION - DATABASES = { "default": { # Ends with "postgresql_psycopg2", "mysql", "sqlite3" or "oracle". "ENGINE": "django.db.backends.oracle", # DB name or path to database file if using sqlite3. "NAME": "--", # Not used with sqlite3. "USER": "--", # Not used with sqlite3. "PASSWORD": "--", # Set to empty string for localhost. Not used with sqlite3. "HOST": <test-database>, # Set to empty … -
scaling django app that relies heavily on database queries
Hello I was load testing my social media platform with LocustIO that uses django 2.1 and SQlite3, and these are my results. Load Testing Results I need to make sure it can run for 10000+ visitors and have looked into caching options like redis. It is a real time app so it involves heavy database usage and power. What road should I go down to make it quicker and scalable? -
How to mock django database connection with specific query
I"m writing a unittest testing my django application. This class has a db query which calls a non-default db (postgresql) and the models have are set to managed=False (this cannot be changed, unfortunately) now the script has a call going like def get(self, request, some_other_stuff): # some script query = """select * from <schema>.<table>.....""" with connections[non_default_db].cursor() as cursor: cursor.execute(query) rs = cursor.fetchall() # some other script I want to mock the rs, but I cannot seem to get the correct module/object to mock. I tried @mock.patch("django.db.backends.util.CursorWrapper", self.cursor_wrapper) where self.cursor_wrapper = mock.Mock(). Didn't work. also tried with mock.patch('psycopg2.connect') as mock_connect: rs = result_set_I_want_to_mock_return mock_connect.cursor.return_value.execute.return_value = rs this doesn't work either. -
Annotate and count django
I have a Word model, it has many to many relation with Phrase model I want to get rid of this loop because it's not effective in terms of performance: for w in words: w.freq = w.phrases.count() w.save() I know I should use annotate and subquery, I have found this question Django: how to annotate queryset with count of filtered ForeignKey field? But don't really understand how I should do it in my case -
Django view with form and objects
I want to create view with form to create post and show post in the same page but i don't know how i can do it because when i add to view.py form i dont see my objects i mean "posts from database" class ProfileView(TemplateView): template_name = 'myapp/profile.html' new_item = get_object_or_404(simplePost) model = simplePost def get_queryset(request): return simplePost.objects.all() def get(self, request): form = HomeForm() return render(request, self.template_name, {'form': form}) def post(self, request): form = HomeForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.save() text = form.cleaned_data['message'] form = HomeForm() return redirect('myapp:profile') def post_new(request): form = HomeForm() return render(request, 'myapp/profile.html', {'form': form}) -
Import function by the use of a period (.) does not work
I am pretty new to python and am using the Django framework to build a webapp. That is why I am following this tutorial online to enhance my knowledge. The current IDE that i am using is sublime text. I have done everything in the tutorial and I am stuck with the following piece of import code(see image). from . import views Image: The problem seems that it would not recognize the period (.) which I tend to use for "from right here within this map, import views". I am not sure what should have caused this issue and I hope I made it clear to u folks what the current issue is, what might be an easy fix. Python version: 3.7.0 Django: 2.1.3 Sublime text: 3.1.1 -
I keep getting a 403 error with Django OAuth Toolkit
I'm using Django OAuth toolkit to restrict access to API and I've followed the tutorial but for some reason DOT is restricting access to every request I make on it. urls.py: from django.conf.urls import url, include from django.contrib import admin from rest_framework import routers from api import views router = routers.DefaultRouter() admin.autodiscover() from rest_framework import generics, permissions, serializers from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^api/v1/', include(router.urls)), url(r'^api/v1/carfax/$', views.GetCarFax.as_view({'get': 'list'}), name='list'), url(r'^api/v1/get_carfax/(?P<pk>[\w-]+)/$', views.GetCarFax.as_view({'get': 'retrieve'}), name='retrieve'), url(r'^api/v1/carfax/create/$', views.PostCarFax.as_view({'post': 'create'}), name='create'), url('o/', include('oauth2_provider.urls', namespace='oauth2_provider')), ] views.py: class GetCarFax(viewsets.ModelViewSet): ''' This view will be used for POSTing new carfax reports to the database ''' queryset = CarFax.objects.all() serializer_class = CarFaxSerializer # authentication_classes = [] permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope, TokenHasScope] #print('TEST') # lookup_field = "vin" def list(self, request): # accessed at url: ^api/v1/carfax/$ queryset = CarFax.objects.all() serializer = CarFaxSerializer(queryset, many=True) return Response(serializer.data) def retrieve(self, request, pk=None, *args, **kwargs): # accessed at url: ^api/v1/retrieve/{pk}/$ queryset = CarFax.objects.all() record = get_list_or_404(queryset, vin__exact=pk) serializer = CarFaxSerializer(record, many=True) return Response(serializer.data) class PostCarFax(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope] queryset = CarFax.objects.all() serializer_class = CarFaxSerializer my requests file: headers = { 'Authorization': 'Bearer 6Lg8UJrv4BQL9WGjqpXhrLreD00Ku2' } data = { "vin": test[0], "structural_damage": test[2], "total_loss": test[1], "accident": test[5], "airbags": 'TESTTTTT', … -
How to add a bunch of defaults to python/django logging
I would like to add in the following for every log entry: user request_method status The only way I've seen to be able to do this is a bit of a hackish way, which is to pass them as args, because that's picked up by the django formatter (I don't see any place where you can pass kwargs -- https://docs.python.org/3/library/logging.html#formatter-objects). Here's an example: log.info("hi", {"user": str(request.user)}) And the following formatter: 'format': '{"message": "%(message)s", "extra": "%(args)s"}', When I parse the log I do something like this: info = { 'message': message } data = ast.literal_eval(extra) for k,v in extra.items(): info[k] = v It seems pretty ghetto -- is there a better way to do this, for example, a way to get the kwargs in the formatter? When I try doing something like: log.info("hi", extra = {"user": str(request.user)}) It either errors, or doesn't pass in the args (obviously). -
Run background task on repeat every N
I'm using django and I want a loop to run in the background let's say every 60 seconds. I found a plugin for Django which seems like it got what I need, but i'm not sure how to make it work. I understand you put the @background in, but as far as I understand django. This goes into a view right? But before the code is being loaded the first time, doesn't the webpage have to be loaded first? I'm talking about the following plugin https://django-background-tasks.readthedocs.io/en/latest/ -
How to make a field editable on create and read-only on update in Django REST framework
I want to implement a kind of constant field of a Django model. I want the field to be set on create a model instance (via REST framework API), but on updating this field must be forbidden to change. Is there an elegant way to do it in Django itself or in REST framework serializer options? -
How to pass extra arguments to the django log formatter
I am trying to do the following: # settings.py 'json': { 'format': 'Avails: {"levelname": "%(levelname)s", "asctime": "%(asctime)s", "funcName": "%(funcName)s", "filename": "%(filename)s", "lineno": "%(lineno)s", "message": "%(message)s"}', }, And in the view: log.info('this is my message', extra={'user': request.user} How would I grab the extra info in the log formatter? -
django column does not exist but no models.py and no changes found
I am completely new to django, but I am trying to change a database table. I read a good amount of post about running python manage.py makemigrations python manage.py migrate But when I run these two command, it does not detect any changes. A solution has been to change the models.py but I do not have that file. (I jumped aboard a working project, which appears to only have initialize the database once at the very beginning. I want to add a column to my .sql file which is copied from an .csv CREATE TABLE test ( columns ... organ_system text <-adding only this line ... more columns); /COPY test FROM 'test.csv' DELIMITER ',' CSV HEADER; I have no clue on where to start/go. The init.py file is completely empty if that means anything.