Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
I changed the session cookie name in my django project and I am unable to login
I want to change the SESSION_COOKIE_DOMAIN because it's set incorrectly on a production server. Due to this, I also need to change the SESSION_COOKIE_NAME to something else. Otherwise there'll be two cookies with the same name and different domains on the users side, because it's already in production. With these two cookies, the user will have trouble logging in. This would normally be fine, but now the problem. Just after I changed the SESSION_COOKIE_NAME (so I haven't even changed the SESSION_COOKIE_DOMAIN yet), I am unable to login into the admin console. I can see the cookie with the new name being set. The application itself has no problems and users can login without problems, which is strange because it's the same authentication. What I've tried: Deleting all session on the server site Private mode Other Browser Flushing all possible caches on the server but might have mist one. Checking for hardcoded session names, but there are not. What else could I possibly check? If I set the SESSION_COOKIE_NAME back to what it was, I can log in again. This issue only happens on the production server; not locally, and on not on the test server, which makes it hard to … -
How to get payment response from paypal as boolean in django?
I am using Paypal api for my Django website.I am able to make payment but don't know how to get payment response from paypal server as either True or False. -
TESTING a Django View
I am very new to testing, this is my first set of tests. I have followed few tutorials on how to go about testing and while the testing that I have made for models and URLs work perfectly, my views testing are not working properly. I have looked at some answers for similar questions on Stack but can't find a reason why this would not work. This is my View function: @csrf_exempt @login_required def profile(request, username): user = request.user profilepic = ProfilePic.objects.filter(user=user).first() username = User.objects.filter(username=username).first() usr = username following = FollowedCrypto.objects.filter(user=user.id) cryptos = [] for i in following: cryptos.append(i.cryptocurrency) return render(request, "PlanetCrypton/profile.html", { "following":following, "user": user, "usr" : username, "profilepic":profilepic, "crypto": cryptos, }) and this is my test for it: def test_profile(self): c = Client() user_b = User.objects.create(username="Fred", password="fred") user_b.save() profilepic = ProfilePic() profilepic.user = user_b profilepic.profile_pic = SimpleUploadedFile(name='test_image.jpg', content=open("/Users/michelangelo/Desktop/FP1/static/images/Jaguar.jpg", 'rb').read(), content_type='image/jpeg') profilepic.save() followedcryptos = FollowedCrypto() followedcryptos.user = user_b followedcryptos.cryptocurrency = "bitcoin" followedcryptos.save() l1 = [] l2 = [] l1.append(followedcryptos) l2.append(followedcryptos.cryptocurrency) data = { "following": l1, "user": user_b, "usr" : user_b.username, "profilepic": profilepic, "crypto": l2 } response = c.get(f"/profile/{user_b.username}", data, follow=True) self.assertEquals(response.status_code, 200) instead of using l1 and l2 to pass the data, I have also directly tried by writing … -
How to add padding downwards in a bootstrap card?
I want to add padding on all four sides of a bootstrap card, and so used the attribute style='padding:25px', but padding is only being applied on the other three sides except downwards, i.e there are no padding between rows. How do I fix this? I am using django and htm <div class="row" style='padding:25px'> {% for player in players %} <div class="col-auto col-md-3"> <div class="column"> <div class="card" style="width: 18rem;"> <img class="card-img-top" width="20" height="300" src="{{ player.image_url }}"> <div class="card-body"> <h5 class="card-title">{{ player.name }}</h5> <p class="card-text">{{ player.position }}</p> <a href="#" class="btn btn-primary">Know More!</a> </div> </div> </div> </div> {% endfor %} </div> -
How to display annotions values in Django templates?
I want to display a list in my dashboard with a context processor view. But it did not works. It displays nothing in the table. But when I use it on another page with a normal view it works. How can I solve it? Is there a problem with context processor? views.py def risk_context_processor(request): current_user = request.user userP = UserProfile.objects.get_or_create(username=current_user) customer_list = Customer.objects.filter(company=userP[0].company) countries = Customer.objects.values_list('country__country_name', flat=True).distinct() country_links = Country.objects.filter() iso_country_links = Customer.objects.values_list('country__country_name', flat=True).distinct() codes = pycountry.countries.get(name='American Samoa').alpha_2 counting_list = Customer.objects.values("country", "country__country_name").annotate( **{ f"{stored.lower().replace(' ', '_')}_count": Count( "risk_rating", filter=Q(risk_rating=stored) ) for (stored, displayed) in Customer.RISK_RATING }, total_credit_limit=Sum("credit_limit"), total_customers=Count("id"), ) test = 0 context = { 'customer_list': customer_list, 'countries': countries, 'country_links': country_links, 'iso_country_links': iso_country_links, 'codes': codes, 'test': test, 'counting_list': counting_list } return context **template.html** <table id="multi-filter-select" class="display table table-striped table-hover grid_" > <thead> <tr> <th>Country</th> <th>Total</th> <th>Low Risk</th> </tr> </thead> <tbody> {% for country in counting_list %} <tr> <td> {{ country }} </td> <td> {{ country.total_credit_limit }} </td> </tr> <tr> <td>{{ country.low_risk_count }} customers</td> </tr> {% endfor %} </tbody> </table> -
Django API, manage linked tabes using multiple GETs
In my django app i expose two tables using DjangoRestAPI for manage data one related to other. There is a result data with this structure: "results": [ { "id": 175194, "device": "f906e9db70b0cc822cb44ccd1b2b89a7", "res_key": "b865c3125cb4ef173e55377026d94b2b", "read_date": "2021-03-31T07:06:04.143569Z", "unit": 2 }, { "id": 21278, "device": "f906e9db70b0cc822cb44ccd1b2b89a7", "res_key": "c8a961f3ef9f8fa0ebdac3c910070055", "read_date": "2021-03-26T15:54:04.171926Z", "unit": 1 }, { "id": 25173, "device": "f906e9db70b0cc822cb44ccd1b2b89a7", "res_key": "75126c6b2b4e78fc553ec75c7eb927ea", "read_date": "2021-03-26T16:48:03.259185Z", "unit": 1 }, ... and a result_details API endpoint related to results: "results_details": [ { "id": 1, "id_res": 236, "var_id": 1, "var_val": "[41]", "var_hash": "6f241d5445cf3031f6420de63c0a409bad527ea3" }, { "id": 2, "id_res": 326, "var_id": 1, "var_val": "[45]", "var_hash": "e5f03cfbed7ee88445b44ddf8e64365da310f8ec" }, ... there is an id_res field that link to main results data. So, in this configuration, every time a user have to get results related to a specific device have to execute a GET to the first table filtering for device and then, inside a for loop, manage n GETs on second table passing the id for link to id_res. If i find 100 rows from Results API call i have to execute 100 different calls to the Results_data. Someone have an idea for better manage this king of situation from external API calls? I try to avoid internal data aggregation, i … -
How to print the contents of the uploaded file in django?
Hi I am beginner in django I was trying to print the content of the uploaded file so far I did is def upload(request): if request.method == 'POST': handle_uploaded_file(request.FILES['file'], str(request.FILES['file'])) return HttpResponse("Successful") return HttpResponse("Failed") def handle_uploaded_file(file, filename): f = open(filename, "r") print(f.read()) I am getting error as FileNotFoundError at /file/upload/ [Errno 2] No such file or directory: 'file.odt' Note: file.odt is the file which is uploaded Please help anyone thanks in advance -
Selenium timing out inconstantly with Django
I have written essentially the same code in two different projects however I'm getting different timed out in the Django project often. My Django project has an endpoint that when hit will start up Selenium webdriver and loop through the products in the database where it finds the urls to scrape. I've been having on and off luck with Article and minimal luck with Wayfair in the Django code but 100% success with the same urls in the minimal code below. Here's my Django code: @api_view(['GET']) def update_prices(request): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36', 'upgrade-insecure-requests': '1', 'dnt': '1' } DRIVER_PATH = '/usr/bin/chromedriver' options = webdriver.ChromeOptions() options.add_argument("--incognito") options.add_argument("--headless") options.add_argument(f"user-agent={headers['User-Agent']}") driver = webdriver.Chrome(executable_path=DRIVER_PATH, options=options) driver.set_window_position(0, 0) driver.set_window_size(1024, 768) XPATHS = { 'wayfair': '//*[@id="bd"]/div[1]/div[2]/div/div[2]/div/div[1]/div[2]/div/div/div/span', 'eq3': "//span[contains(@class,'MuiTypography-root') and contains(@class,'MuiTypography-h3')][1]", 'elte': '//*[@id="mainContent"]/div[1]/div/div[3]/div/div[1]/div/span', 'crate_&_barrel': '//*[@id="react_0HM7EN3OE6904"]/div/div/div[2]/div[4]/div[1]/div/div[2]/span/span/span', 'bouclair': '//*[@id="ordering-panel"]/div[1]/div[3]/div[3]/div/span/span[2]/span', 'article': '//*[@id="app"]/div[2]/div/div/div[4]/div[4]/div[2]/div[2]/div[2]/div[2]/span' } for product in Product.objects.all(): # Skip products without URL if not product.url: pass url = product.url print('Getting:', url) driver.get(url) print('Got URL') def wait_for_element(retailer): try: return WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, XPATHS[retailer]))).text except TimeoutException: return 'Timeout' if ('wayfair.ca' in url or 'wayfair.com' in url): print("Retailer: Wayfair") retailer = 'wayfair' price = wait_for_element(retailer) elif ('eq3.com' in url): print("Retailer: EQ3") retailer = … -
Using Django alllauth to sign in with ACCOUNT_EMAIL_VERIFICATION gives incorrect redirect
I am using Django allauth to manage users on my website. I want to use ACCOUNT_EMAIL_VERIFICATION and so in settings.py ACCOUNT_EMAIL_VERIFICATION = 'mandatory' # problematic - changes login redirect! LOGIN_REDIRECT_URL = '/home' home.html {% if user.is_authenticated %} ... {% else %} <p>You are not signed in</p> <p><a href="{% url 'account_login' %}">Sign In</a></p> ... {% endif %} If I comment out ACCOUNT_EMAIL_VERIFICATION = 'mandatory', sign in works as expected and redirects to '/home' If I leave it in, it redirects to accounts/confirm-email/ I have tried to find where the redirect occurs in the allauth code, but I cannot find it. Can anyone help? -
TypeError: 'InMemoryUploadedFile' object does not support indexing
I have a list of Document display in my browser, all documents that the user submit will save in my database, this is my views.py h = 0 documentname=[] for docname in request.POST.getlist('docname'): documentname.append(docname) //name of the document for i in documentname: for docfile in request.FILES.getlist('doc'): //the file of document clientsubmitdocument = clientSubmittedDocument( User=V_insert_data, Document=docfile[h], Remarks=documentname[h] ) clientsubmitdocument .save() h+=1 print("clientsubmitdocument ",clientsubmitdocument) my html {% for doc in docpergradelevel %} <tr> <td style="border-radius: 5px; border: 1px solid #ddd; "> <input type="file" name="doc">{{doc.Document_Requirements}} <input type="hidden" name="docname" value="{{doc.Document_Requirements}}"> {% endfor %} this is the error i encountered Traceback (most recent call last): File "C:\Users\Kaito\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\Kaito\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Kaito\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "E:\UCC\unidaproject\accounts\views.py", line 630, in elementaryEnrollment Document=docfile[h], TypeError: 'InMemoryUploadedFile' object does not support indexing -
Django - django.db.utils.OperationalError: no such table: django_session
I am trying to use AbstractUser in django. I made migrations and migrated and everything goes fine but then i tried to login using admin and i got the Following Error- no such table: django_session Request Method: GET Request URL: http://127.0.0.1:8000/admin/ Django Version: 3.1.7 Exception Type: OperationalError Exception Value: no such table: django_session Can anyone help how should i fix this? I have also added AUTH_USER_MODEL in settings.py -
How to filter django queryset by regex for a phone number in any format?
Users in my postgresql database have field that stores phone numbers in different formats, like: 34567889 +34567889 +(345)67889 or even +(345) 678 89 Having the string with only digits (34567889) I want to be able to find a record no matter of format. I am trying to use regex search for this (with no luck), that looks like this: queryset.filter(phone__regex=r"^.*?3.*?4.*?5.*?6.*?7.*?8.*?8.*?9$") Same regex works for me outside the django orm, in interpreter, for instance, but does not work when I do queryset filter. Any ideas what I am doing wrong? Any help will be appreciated. Thanks in advance. -
Python Django script throwing an error -HTTPConnectionPool Max retries exceeded
I have Django App and I am trying to get data from our server. The code is as below: def login(request): #global userlist, login_data if request.method == "POST": # get email or mobile_number and password from login.html screen email_id = request.POST.get("your_name") password = request.POST.get("your_pass") try: login_data = requests.get('http://<our URL>/api/usersapi/?mobile_number=' + email_id + '&password=' + password, timeout=0.05).json() # get user api json data if len(login_data) == 1: # check dictionary of login_data is empty or not for x in login_data: user_type = x['user_type'] user_id = x['user_id'] request.session['login_data'] = user_id request.session["admin_status"] = True return render(request, 'gui/dashboard.html', {'user_type': user_type, 'login_data': login_data}) else: # if correct details then return to dashboard page return render(request, 'gui/login.html', {'msg': 'Wrong Username and Password'}) except requests.exceptions.ConnectionError as e: print(e) r = "No response" return render(request, 'gui/login.html', {'msg': e}) return render(request, 'gui/login.html') It throws an error while establishing the connection. HTTPConnectionPool Max retries exceeded with (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f3b389becc0>: Failed to establish a new connection: [Errno 111] Connection refused', I have already seen and applied many solutions related to the same error. But, I am not able to figure out the problem. Our system ulimit-> 65536 I have configured settings using WSGI. and /etc/hosts has our url … -
Creating a notification system in Django
so I am building the backend for a web forum using Django & Django REST Framework and would like to include a notification system. So I have a "Discussion" Model: def user_directory_path(instance, filename): return f'{instance.id}/{filename}' class Discussion(models.Model): title = models.CharField(max_length=250) content = models.TextField(max_length=10000) date_created = models.DateTimeField(auto_now_add=True) date_updated = models.DateTimeField(auto_now=True) is_pinned = models.BooleanField(default=False) is_open = models.BooleanField(default=True) is_boosted = models.BooleanField(default=False) started_by = models.ForeignKey(to=User, related_name='started_discussions', on_delete=models.CASCADE) subscribers = models.ManyToManyField(to=User, related_name='subscribed_discussions') tools = models.ManyToManyField(to=Tool, related_name='belongs_to_channels', blank=True) channels = models.ManyToManyField(to=Channel, related_name='discussions') def __str__(self): return f'{self.id} | By: {self.started_by.username} | Content: {self.title} ...' and I have a "Comment" Model: class Comment(models.Model): content = models.CharField(max_length=5000) discussion = models.ForeignKey(to=Discussion, related_name='comments', on_delete=models.CASCADE) written_by = models.ForeignKey(to=User, related_name='written_comments', on_delete=models.CASCADE) liked_by = models.ManyToManyField(to=User, related_name='liked_comments', blank=True) def __str__(self): return f'{self.id} | By: {self.written_by.username} | {self.content[0:30]} ...' Basically my question is that I would like to create a functionality where if someone creates a new comment on a discussion, the users that have subscribed to this discussion will get a notification. So for example if someone comments on a discussion you are subscribed to, you'll get a notification "New Comment from {user that commented} on {discussion you're subscribed to}" My thought was to create a new django app "Notification" with the respective model and … -
'NoneType' object has no attribute 'append'..why is this the case since i did not assign that statement to another variable
AttributeError 'NoneType' object has no attribute 'append' def func(request): if request.method == 'POST': html = request.POST.get('html') email = request.POST.get('email') first_html = BeautifulSoup(html, features='html.parser') second_html = BeautifulSoup(''.join(open('file1.html'))) for element in second_html: first_html.body.append(copy.copy(element)) I am trying to concatenate 2 html using Beautiful Soup, However i receive an error in the line: first_html.body.append(copy.copy(element)) why is this the case since i did not assign that statement to another variable -
How do I make Django form fields only editable if they're blank?
The use case: It's kind of like a signup sheet. The form has 3 fields, each of which should only be editable if they were blank when the form was requested. Once the form is submitted, these fields for this specific instance of the model shouldn't be editable. The question: How do I do this? I was thinking of using Javascript to set the fields to editable if they already have something in there, but I'm almost certain there's an easier way. -
Wagtail Admin Filter Button
I'm asking 'cause I haven't found such information in docs. I've read than construct_explorer_page_queryset hook allows to filter queryset. But it does on a constant basis. Is it possible to create button to turn on/off this filter. For example, I have Blog Page (parent page), than includes Posts, Tags, Categories Pages (children). And Blog Page listing shows me all types of these pages. I want to filter them by buttons Only Categories, Only Tags, Only Posts. Sure, I know than there is ModelAdmin feature. But it doesn't fit my requirements due to some issues with localize. I have to use only Page Explorer. If I just read heedlessly and this information can be found in docs - please, point me. Thanks! -
Why is there a difference in the output of pythonanywhere and local visual studio code?
I am trying to make a speech to text program in python and deploying it with django . On the same audio file I am getting different results when I try to run it on pythonanywhere and visual studio code Here is my code : import speech_recognition as sr def aud(): f = 'filepath/output.wav' r = sr.Recognizer() x = sr.AudioFile(f) with x as source: audio = r.record(source) mytext= r.recognize_google(audio) return(mytext.lower()) VSC Output: "ok around better late than never ad-hoc" Pythonanywhere Output: "oakwood aren't naturally at home" How can I get the same result ? -
understanding transaction_db fixture in pytest-django
Python 3.7 Django 3.1 PostgreSQL 12.6 I'm fairly new to unit tests, especially in django. I'm using pytest along with pytest-django to manage my tests. I came across a global fixture in pytest-django called transactional_db. My understanding, so far, is that if you want transactions to be rolled back, if something goes wrong during tests run, then use transactional_db. Also, Doc says This fixture can be used to request access to the database including transaction support. This is only required for fixtures which need database access themselves. A test function should normally use the pytest.mark.django_db() mark with transaction=True to signal it needs the database. Blockquote I'm also using factory to create ModelFactory to be used in unittests. something like below- class MyModelFactory(factory.django.DjangoModelFactory): title = factory.LazyAttribute(lambda x: faker.name()) class Meta: model = MyModel As I mentioned above, doc says “This is only required for fixtures which need database access themselves”. but if I understand correctly, the Modelfactory also accesses the Database. I mean Modelfactory create/update objects in the DB. isn’t it true? This is what confuses me, what is the definition of access to the database? What's the use of transactional_db in pytest? In My case, I have a simple REST … -
Updating all instances of a model everyday in Django
First of all, I know this is not a general "stackoverflow" type of question, but I can't find another good website to ask this. I personally think this is a perfectly reasonable question, so I hope you can help me out here. Basically how can you make all the model instances updated on a daily basis in Django? Right now, I have a model that has each of my students' information. I want to update each of my students information everyday, so that the correct students will show up in my schedule list in django daily. For example, if it is a holiday, then a "show in my list" field in each student's model will be updated to false. Of course, I am going to add a lot of functions such as "is it a holiday today?", "does the student have a class with me today?", "at what time does the student have class with me today?", etc. If the student matches all of the requirements(or functions) to "show up" in my schedule, then the "show in my list" field in each student's model will be updated to true. My schedule list will then loop through all of my students … -
key "article" which have some long string value right? So I want you to divide it into parts
http://139.59.86.56/coursesetup/api/4 Do you see keys and values? So there is a key "article" which have some long string value right? So I want you to divide it into parts. For example: "key1": "This is the first line" "Key2": "This is the second line" Not only two, as many lines as it has and before that give me a number of parts like "no_of_parts": "5" This means it contains 5 lines or 5 parts. How can I do this? Thanks -
Unable to translate
I am using Django and angular and in my template I have that : <table class="table"> <thead> <th>{% trans 'Name' %}</th> </thead> <tbody> <tr ng-repeat="word in allwords"> <td>{% trans word.name %}</td> </tr> </tbody> </table> This line is translated perfectly <th>{% trans 'Name' %}</th> But that line is not translated at all : <td>{% trans word.name %}</td> I precise I tried blocktrans also but without effects. Of course I did the compilemessages to test that. Could you help me please ? Thank you very much ! -
leaves matching query does not exist [closed]
leaves = Leaves.objects.get(user=user,leave_type=leave_type) balance = leaves.balance print(expanded, days) emp = Employee.objects.get(user=user) m_eid = emp.team.manager_eid manager = Employee.objects.get(eid=m_eid) email = manager.user -
How to reset a password with the implemention of the forms in django?
Here, I'm creating a custom password reset page with custom form in Django. Also, I use django-rest-resetpassword package to generate reset tokens and send it via mail. Once the user clicks the link it verify the token for expiration, used-ones and if the tokens are good so the user can change the password. say the url is reset-password/verify-token/?token=123tq24uig32h it redirects to reset-password/verified/ reset-password/verified/ This page shows the template(i.e.,HTML page) for new password and confirm password. When the tokens are good it is redirected to a template. So, There may arise a question that if the user already changed the password when he knows the url user can visit this page reset-password/verified/ to prevent is I also passed token to verified page using get method and verify the status of the token. Now I need to validate the form-template of new password and confirm password The code for token verificaiton is here class CustomPasswordTokenVerificationView(APIView): """ An Api View which provides a method to verifiy that a given pw-reset token is valid before actually confirming the reset. """ throttle_classes = () permission_classes = () parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,) renderer_classes = (renderers.JSONRenderer,) serializer_class = CustomTokenSerializer def get(self, request, *args, **kwargs): toks = … -
How do I get multiple rows from django database? [duplicate]
Hello I am trying to create an employee attendance program and I want to make it so that the employees can see the total hours they worked. This is the code that creating the problem: elif 'checkName' in response.POST: if form.is_valid(): n = form.cleaned_data["name"] t = Name.objects.filter(name=n) totalHours = datetime.combine(t.date, t.timeOut) - datetime.combine(t.date, t.timeIn) messages.success(response, totalHours) return redirect('/') The error I get is 'QuerySet' object has no attribute 'date' and if I use t = Name.objects.get(name=n) it shows an error that says 'get() returned more than one Name -- it returned 2!'