Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Web application - file storage - security
Background: I have a large shared disk area where standard unix groups are used to control access; i.e. to access disk area: /storage/X you have to be member of the group which owns the directory /project/X. I know want to make a web application where people can post rawdata from the /project/X directory to a webserver, and then view processed data from a rest API. The rest API should have the same access control as the underlying disk area; i.e. if and only if you have file system access to /project/X you should have access to the corresponding http endpoint. My plan was to associate the unix group name with the uploaded files, and then check that the user accessing http[1] was member of this group. I.e. in Django the model would be something like: class Content(Model): group = CharField( ) resource = ??? def access(self, user): linux_group = grp.getgrnam(self.group) return user in linux_group.gr_mem It seems to work, is extremely simple, but my experience with security is quite limited and I might be completely out on a limb? Comments greatly appreciated. [1]: The user name is assumed to come from a Single Sign On scheme in front of the web … -
Error running django local server: `__init__() got an unexpected keyword argument 'tcp_nodelay'`
Everything was fine and always run without problem. Now I get the error when I load the app from the browser. Seems not something related to the code because I jumped on a oldest branch and I get the same error. Here my python and django version $ python --version Python 3.6.0 $ python -c "import django; print(django.VERSION)" (1, 11, 5, 'final', 0) Here the error when I try to load the 127.0.0.1:8000 url Traceback: File "/Users/karim/Envs/ec/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request) File "/Users/karim/Envs/ec/lib/python3.6/site-packages/django/core/handlers/base.py" in _legacy_get_response 244. response = middleware_method(request) File "/Users/karim/Envs/ec/lib/python3.6/site-packages/django/middleware/cache.py" in process_request 133. cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache) File "/Users/karim/Envs/ec/lib/python3.6/site-packages/django/utils/cache.py" in get_cache_key 362. headerlist = cache.get(cache_key) File "/Users/karim/Envs/ec/lib/python3.6/site-packages/django/core/cache/backends/memcached.py" in get 79. val = self._cache.get(key) File "/Users/karim/Envs/ec/lib/python3.6/site-packages/django/core/cache/backends/memcached.py" in _cache 172. self._client = self._lib.Client(self._servers, **client_kwargs) Exception Type: TypeError at / Exception Value: __init__() got an unexpected keyword argument 'tcp_nodelay' Any suggestion where to look to fix this nasty issue? Thank you -
Django app as microservice
I want to use the django project as one microservice REST application without any auth module. After I disable the auth and admin modules I go the error when I start the project. /django/db/models/base.py", line 118, in __new__ "INSTALLED_APPS." % (module, name) RuntimeError: Model class django.contrib.auth.models.Permission doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. How can use django project as microservice REST application ? Thanks -
Enable LTI Provider Functionality OpenEdx
I am following this document for adding ITI in openedx http://edx.readthedocs.io/projects/edx-installing-configuring-and-running/en/latest/configuration/lti/enable_lti.html but after run migration ./manage.py cms --settings=devstack migrate ./manage.py lms --settings=devstack migrate these tables are not created in database. lti_provider_gradedassignment lti_provider_lticonsumer lti_provider_ltiuser lti_provider_outcomeservice -
IntegrityError on model with two m2m fields
This is what my model looks like: class Interaction(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) author = models.ForeignKey(Staff) datetime = models.DateTimeField() tags = models.ManyToManyField(Interaction_Type) comments = models.TextField() related_clients = models.ManyToManyField(Client, related_name="interactions", blank=True) related_staff = models.ManyToManyField(Staff, related_name="interactions", blank=True) active = models.BooleanField(default=False) When I try to save an instance of this model, I get this error: django.db.utils.IntegrityError: insert or update on table "interactions_interaction_related_clients" violates foreign key constraint "interact_interaction_id_4927567c_fk_interactions_interaction_id" DETAIL: Key (interaction_id)=(85eb4cff-1be8-45f7-af40-459418ec6364) is not present in table "interactions_interaction". It feels like that Django is trying to write to the pivot tables before writing to the main Interaction, Staff and Client tables. Is there any remedy to this? -
Django + Apache with wsgi got "Internal Server Error"
I got "500 Internal Server Error", when I try to connected my Django project to Apache Server. I try lots of way to setting the configuration file, include some method on stackoverflow. But I still can't solve the problem, can you give me some advance? Thank you very much! My django project dir like this: |django |--MySite |--MySite |--wsgi.py |--django.wsgi |--urls.py |--settings.py |--Blog |--__init__.py |--views.py |--models.py My setting as below: 1.httpd-vhosts.conf: <VirtualHost *:8080> ServerName local.Test WSGIScriptAlias / /Users/Hao/django/MySite/MySite/django.wsgi <Directory /Users/Hao/django/MySite/MySite> Options Indexes MultiViews AllowOverride All Require all granted <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost> 2.django.wsgi: import os, sys sys.path.append('/usr/local/django') sys.path.append('/Users/Hao/django') sys.path.append('/Users/Hao/django/MySite') os.environ['DJANGO_SETTINGS_MODULE'] = 'MySite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() 3.Finally, I open 127.0.0.1:8080 in browser, and I got "500 Internal Server Error". Thanks. -
two django server in one project
I am writing one project that have two server, one is management node, one is agent, all both is http server. Now should I just create two django project for them? and only use one git repository. But I found there are some code common that all used by two project. How to organize two project is more better? -
Serve standalone django app with uwsgi
I am trying to run a django app that is already packaged. I already have a tar.gz file of it by running python setup.py sdist where all the dependencies are installed on build process. I have a uwsgi .ini file that looks like this. [uwsgi] vhost = true chdir = /var/www/uswgi_app module = django_packaged_app.config.wsgi:application plugin=python3 env = DJANGO_SETTINGS_MODULE=config.settings.local stats = 127.0.0.1:1717 # try to autoload appropriate plugin if "unknown" option has been specified autoload = true # enable master process manager master = true # automatically kill workers on master's death no-orphans = true # bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket socket = /run/uwsgi.socket # set mode of created UNIX socket chmod-socket = 660 chown-socket = www-data # place timestamps into log log-date = true # user identifier of uWSGI processes uid = www-data # group identifier of uWSGI processes gid = www-data Where my wsgi.py file is on the following project structure: |-django_packaged_app |-config |-settings |-local.py |-wsgi.py This django_packaged_app is not a conventional directory served in /var/www but a module that can be installed with pip when provisioning my server. On /var/ww/uswgi_app is where I have my .python-eggs file that caches packages. The concrete problem I have is when … -
Django Admin form automatic log the committed user
My schema has created_user, and updated_user That is find to work with ordinary user. I do define views and forms. And fill up them easily. Problem: How can I use the same logic apply to DjangoAdmin page? It has no views for me. Reference: https://docs.djangoproject.com/en/1.11/intro/tutorial07/ -
Python - DJango SES
I am new at Amazon API(s) and I need to develop a python program that can receive email from Amazon SES and upon searching i found this DJANGO module and upon reading at their documentation(http://django-ses-gateway.readthedocs.io/en/latest/) about receiving an email I can't understand where should i get the request.body here: from django_ses_gateway.receiving_mail import sns_notification subject, from_mail, to_mail, hash_code, mail_content = sns_notification(request.body). I Hope someone can help me and thank you in advance. -
RelatedObjectDoesNotExist at /profile
I have created the user profile model. I migrated the model to syn with the database. However I am getting an error of User has no profile when I route to /profile (whose url is this url(r'^profile$', views.update_profile, name="profile"),) which should call the update_profile view where I have printed the request object before checking if its a POST method or not but it is also not shown in shell. Where have I done wrong? Here is my code class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=600, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(blank=True, null=True) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) post_save.connect(create_user_profile, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save() post_save.connect(save_user_profile, sender=User) class UserForm(forms.ModelForm): class Meta: model = User fields = ('first_name', 'last_name', 'email') class ProfileForm(forms.ModelForm): class Meta: model = Profile exclude = ('user', ) def update_profile(request): print('user ################', request.user, request.user.profile) if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm( request.POST or None, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success( request, ('your profile was successfully updated!')) return redirect('products:profile') else: messages.error( request, ('There was an error updating your profile')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'dashboard/company/profile.html', {'user_form': user_form, 'profile_form': profile_form}) -
How to define a nested list in django for json response
I am new to Django and so I thought of creating basic app that represents operations in a shipping company. I have a WorkOrder which contains Shipments. So my models.py contains the following : class WorkOrder (models.Model): status = models.CharField(max_length=300,default = "New") source = models.CharField(max_length=300) destination = models.CharField(max_length= material = models.CharField(max_length=300) shipmentlist = [] //PROBLEMATIC CODE class Shipment (models.Model): expected_startDate = models.DateTimeField(default=timezone.now) expected_endDate = models.DateTimeField(default=timezone.now) shipment_status = models.CharField(max_length=300,default = "Not Started") I have 2 serializers WorkOrderSerializer and ShipmentSerializer which i have defined in serialzers.py. I want to return a list of shipment contained within a Work Order object. class WorkOrderSerializer generated_date = models.DateTimeField(default=timezone.now) status = models.CharField(max_length=300, default="New") source = models.CharField(max_length=300) destination = models.CharField(max_length=300) material = models.CharField(max_length=300) shipmentlist = ShipmentSerializer(many=True) class ShipmentSerializer expected_startDate = models.DateTimeField(default=timezone.now) expected_endDate = models.DateTimeField(default=timezone.now) shipment_status = models.CharField(max_length=300, default="Not Started") I am following the model specified here. http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects In my views.py I am calling the serializer when I get the request like this def workorder_operations(request,workorder_pk): workorder = Work_Order.objects.filter(pk=workorder_pk) serializer = Work_Order_Serializer(workorder) What this produces is a json that looks like the following { "shipmentlist":[] } I am completely confused about two things : 1. why it shows ONLY shipment object and not the others even if it cannot … -
templates extensions cannot work when I add another tag name
In my base.html I have add a template named{% block tags},and corresponding tag in my tag.html,I really confusing why it doesn't work -
An IndentationError of Python Django code?
I met a problem when I learn Django1.11.5(python2.7) as a beginner. There are urls.py, views.py and so on. Here is views.py: ## views.py from django.http import HttpResponse import datetime def hello(request): return HttpResponse("Hello World") def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now return HttpResponse(html) And here is urls.py: ## urls.py from django.conf.urls import url from django.contrib import admin from mysite import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^hello/$', views.hello), ##name='home' is not necessary url(r'^time/$', views.current_datetime), ] When I used the command: python manage.py runserver, there is a mistake displayed: html = "It is now %s." % now IndentationError:unexpected indent. I checked space and and tab and coundn't find mistakes. If I change the line "now = datetime.datetime.now()" to "now = 1234", there will be no mistake. Also I found if the last line includes parenthesis, the next line will have IndentationError(Even for functions like round(2.5)). I cannot figure this problem out, can anybody help me? Thank you very much!!! -
function for custom field's default value based on related models
How shall I define a function for field's default value that returns a string with this pattern: [id] a parent model's field [TextField] current model's field, excluding silent letters (a, e, i, o, u) should be joined with dash (-) should be unique before sending to field # Grandparent Model class GrandParent (Model): # Parent Model class Parent (Model): grand_parent = OneToOneField(GrandParent, primary_key=True) # Child Model class Child (Model): def child_code(): /* What params should be passed? */ excludes = 'aeiou' child_name = child_name.translate(None, 'excludes') return '-'.join(child_name, Parent.grand_parent) id = CharField(primary_key=True, default=child_code, editable=False, max_length=12) parent = ForeignKey(Parent, on_delete=CASCADE) child_name = CharField(max_length=32) And I understand that Parent.grand_parent isn't the right way to call it. Also I think that max_length should be checked within the function. -
django doesn't server user-uploaded files in production
I deployed a website with django. it servs all static files but doesn't serve user-uploaded files. this is my settings.py and urls.py: import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEBUG = False STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticroot') STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] MEDIA_URL = '/files/' MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded_files') and the urls.py: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/$', LoginView.as_view(), name='login'), url(r'^register/$', RegisterView.as_view(), name='register'), url(r'^logout/$', logout_view, name="logout"), url(r'^api/', include('main.api.urls')), url(r'^class/', include('main.urls')), url(r'^', include('accounts.urls')), url(r'^$', Index.as_view(), name='index'), ] In the pages, it prints user-uploaded files addresses correctly, but doesn't show them anf when I click on the links, it shows me 404 page. I use last version of nginx that I took from nginx repositories. the version 1.12. is it related to django or webserver(nginx+ gunicorn)? and how can I solve the problem? -
Django rqworker running job with dynamic function name; module has no attribute zzz
I have a Django server; the client pulls datasets from API endpoints and sometimes the dataset is large, and generating it takes time. For this reason I cache the output. I don't want to wait for the data to be generated and then cached to return a Response, so I spin a background job and return immediately with a HTTP 202 (Accepted: the requests was received to process, but not processed yet) The client receiving the 202 will retry with a delay until it gets the data, but the server is not under load. I got this working as a wrapper around the endpoint processign function. Because RQWorker expects a serializable set of parameters, I am passing the function as a path to the function in my module (i.e. 'dashboard.api.get_data_xyz') In the job, I lookup and run the function by importing the module, and parsing down to the function I need, with path = function_path.split(".") f = __import__(path[0]) if len(path) > 1: for sub_module in path[1:]: f = getattr(f, sub_module) Now, my problem is that when I run this with ASYNC=False mode for testing, my function is found (the code runs on the actual server, synchronously, no problem), but when … -
How to group rows containing similar element from a table using MYSQL/ Python / Django?
I have a table being rendered in my HMTL page. The data comes from a MYSQL query and being rendered using a loop. Let’s say I have this table: I'm able to get these data by models -> views - > html views: def context(request): context = { "contents": Something.objects.get_contents() } return render(request, 'contents.html', context) Notice that the only repeating priority is a type of ‘news.’ I am expecting 4 news to show up and they can share similar priorities. Any other genre should have their own priority. How can I write a query and render them to an html page to have an output like this: I appreciate all the help! Thank you. -
Invalid message, Invalid parameter - Djang - Dwolla
I am working on integrating the Dwolla api service with my django project. I got the hang of it and the integation process. For some reason, I am getting an error and I am not sure how to fix the error or what exactly the error means in the schema. I am trying to link a funding source to a user by giving the account number, routing number, name, and account type. But there is a problem with the parameters that I am passing I think. I will attach my code, Dwolla sample code, and the response. My processing code: def linkAccountDwolla(request, form): currentUser = loggedInUser(request) currentProfile = Profile.objects.get(user = currentUser) cd = form.cleaned_data routingNumber = cd['routingNumber'] accountNumber = cd['accountNumber'] account = cd['account'] name = cd['accountName'] customer_id = currentProfile.dwolla_id print(customer_id) request_body = { 'routingNumber':routingNumber, 'accountNumber':accountNumber, 'type':account, 'name':name, } print(request_body) customer = app_token.post('%s/funding-sources' % customer_id, request_body) customerLocation = customer.headers['location'] print(customerLocation) Here is the return that I am getting as well as the error: ValidationError at /link_account/ {"code":"ValidationError","message":"Validation error(s) present. See embedded errors list for more details.","_embedded":{"errors":[{"code":"Invalid","message":"Invalid parameter.","path":"/routingNumber","_links":{}}]}} Request Method: POST Request URL: http://127.0.0.1:8000/link_account/ Django Version: 1.11.5 Exception Type: ValidationError RETURNED VALUES: csrfmiddlewaretoken 'UZIcuNubcPGAAclix2mrNtgvDXqzyc43m2SE4zxqAQljzepuL37865hzCmXeyabc' account 'checking' accountName 'omars accoun' routingNumber '123456789' accountNumber … -
Complex Q Lookup
Here's the code, user_obj = Q(first_name__icontains=query) | Q(last_name__icontains=query) This looks for either first name or last name but if i'm searching for both at a time no results are shown. How can I make a query for 'first name', 'last name' & also 'full name'? -
Cannot connect to mysql image by linking in docker compose file
I am using the following docker-compose file version: '2' services: app_test: build: context: . dockerfile: Dockerfile-jenkins-test ports: - "7200:7200" volumes: - .:/opt/project environment: - DJANGO_SETTINGS_MODULE=myapp.settings.test - MYSQL_DATABASE=test_db - DB_HOST=mysql_test_db - MYSQL_ROOT_PASSWORD=my_pass - DB_PORT=3306 links: - mysql_test_db mysql_test_db: image: mysql:latest container_name: mysql_db_container expose: - "3306" environment: - MYSQL_ROOT_PASSWORD=mypass - MYSQL_DATABASE=test_db when trying to access the mysql using the DB_HOST mysql_test_db it will gives an error saying django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'mysql_test_db' (0)") How to access the linked MySQL db image from app_test? -
In django, does "migrate" just do database stuff?
I understand migrate sets up the database according to the migration, but the scope of it's operation seems to go beyond that. When I ran manage.py migrate, not only did it apply the database migrations, which is good, but it also went through the main url.py and started to execute the methods I had in there which I would like only to be executed only once when the "web starts". What does migrate actually do? Thanks! -
Django: Get pk from previous url
I would like to get the pk from the url that was before, for example: /instructor/1 /instructor/1/sex/2 the problem is that inn url.py displays a new url when sex is selected and i cant save the instructor's pk. this is url.py from django.conf.urls import url, include from . import views from django.conf import settings from django.conf.urls.static import static urlpatterns = [ url(r'^$', views.InstructorListView.as_view(), name='instructor_list'), url(r'^instructor/(?P<pk>[\w-]+)$', views.SexoListView.as_view(), name='sexo_list'), url(r'^instructor/(?P<pk>[\w-]+)/sexo/(?P<pk2>[\w-]+)$', views.RutinaListView.as_view(), name='rutina_list'), url(r'^detail/(?P<pk>\d+)$', views.InstructorDetailView.as_view(), name='instructor_detail'), ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) this is views.py from django.shortcuts import render from rutinas.models import Instructor, Rutina, Sexo from django.views.generic import ListView, DetailView from django.shortcuts import get_object_or_404 from django.template import loader from django.http import HttpResponse # Create your views here. class InstructorListView(ListView): model = Instructor class InstructorDetailView(DetailView): model = Instructor class RutinaListView(ListView): model = Rutina # Filtro de rutinas deacuerdo a la seleccion de instructor y sexo def get_queryset(self): inst = Rutina.objects.filter(sexo=self.kwargs['pk2'], instructor=self.kwargs['pk']) return inst class SexoListView(ListView): model = Sexo For test i have the folowing in a sex_list template <a href="{% url 'rutina_list' pk=1 pk2=sexo.pk %}"> {{ sexo.genero }} </a> i just what that pk is equal from the pk that was selected in instructor_list, but it displays a new webpage and pk is refreshed. any ideas ? … -
Django. Using .ini files in code folder
I wanted to have a .py file using a ConfigParser class to recover information from a .ini file (containing URLs for a 3rd party REST service) Until now this worked since it was finding the file, but now it's not. App structure is: app folder file.py urls.ini I assume this is due to my static files settings, which are as following: STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' # Extra places for collectstatic to find static files. STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) This is because I wanted to mantain settings that were compatible for both local development and web production escenarios (for which I could probably test with code in the web) for Heroku. I wanted to make it so I didn't have to change much stuff. Thanks in advance. -
How to run this script in django correctly? (mqtt script)
I use mqtt paho, the way here is explained. First if I run the development django server, that gives me 2 scripts of mqtt.py runing, unless I run the server with --noreload python manage.py --noreload That gives me just 1 mqtt script running. But in production I have all this setup with django, gunicorn, nginx, and postgress. So the issue is that gunicorn runs 3 times my mqtt.py file I think that its directly atached to the number of workers in my gunicorn configurations(it is 3 workers). I need to know how do I run this script just 1 time? My mqtt file is suscribed to a topic that writes in a data base that topic, so if the script is runing 3 times, I get 3 database new objects each time I publish 1 message. I think I am calling in the wrong place the script. Of course I need a solution without changing the 3 workers in gunicorn to 1. mqtt.py import paho.mqtt.client as mqtt # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) # Subscribing in on_connect() means that if we lose …