Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django LDAPSearch Debug
I'm having troubles grouping users in Django. I can authenticate fine until I try group users. When I add AUTH_LDAP_GROUP_SEARCH I'm not able to login any longer with LDAP users and I get INVALID_CREDENTIALS: AUTH_LDAP_GROUP_SEARCH = LDAPSearch("cn=groups", ldap.SCOPE_SUBTREE, f"(objectClass={AUTH_LDAP_GROUP_SEARCH_ROLE})" ) AUTH_LDAP_GROUP_TYPE = PosixGroupType() The groups are posixGroups, the objectClass is correct on the search and are the groups are under cn=groups. Can I iterate over the AUTH_LDAP_GROUP_SEARCH results to ensure that it's returning correctly? What's the best way for me to debug this? -
Elements in List of List (django python HTML template engine)
I am really trying for 2 hours now and used the search, but i dont know what i am doing wrong Lets say i have a list of lists. Every list in the list has a variable count of items. timelist = [['12:00', '12:30'], ['13:00', '13:30', '14:00']] How can i get the value of each element... According to the doc i have to do it like this (i want to add the values into a table): {% for everylist in timelist %} {% for a in everylist %} <td>{{ a }}</td> {% endfor %} {% endfor %} But it doesnt work. What am i doing wrong? EDIT: I want to insert the value of the time into a website (e.g. 12:00). I am creating a web calendar. The value of the time shall be written into a table. The values are random. -
How to store user location disregarding language
I'm trying to develop a kind of ""simple"" social network using Django and Postgres. Users can signup by providing name, email and address. The address is a set of other fields: street, city, state, country. I want to provide an experience like Airbnb provides. Users can search for a city, state and country in any language they want. But I have to store it in a retrievable way to build reports and data analysis. For example, if someone search and register himself as citizen of 東京, I should be able to include this user on reports about Tóquio, or Tokyo. In other words, system has to know about cities disregarding the language. What I did is download a huge database with city names and aliases, but the maintenance is a pain. The database lack some cities and information. Also, this pain happens with state and countries too. Create development environments and migrate data almost lead mates to suicide. Actually, I'm thinking about drop my current cities database and tie it to a service like Google Maps, but I'm not sure if it will solve my reports requirements. How to create reports by country, state and cities using Google. I'm afraid … -
DJANGO download object has no attribute 'product_image_url'
I have I big database with images and i want to use download in my site where the user to can download personal images. i will try to use download method from this question(without sure if that is correct) is the fist time to try using download method in my site. but I take this error message : AttributeError at /download/125/ 'MyModel' object has no attribute 'product_image_url' here my code : models.py : def user_directory_path(instance, filename): return 'user_{0}/{1}'.format(instance.user.id, filename) class MyModel(models.Model): name = models.TextField() slug_name = models.SlugField() upload = models.ImageField(upload_to=user_directory_path) views.py: def download_image(request, id): descripts=MyModel.objects.filter(user=request.user) product_image =get_object_or_404(descripts,pk=id) print product_image product_image_url = product_image.product_image_url() image_url = MyModel.objects.get(descripts,pk=id).image_code_url() print product_image_url wrapper = FileWrapper(open(settings.MEDIA_ROOT+ product_image_url[6:], 'rb')) print wrapper content_type = mimetypes.guess_type(product_image_url)[0] response = HttpResponse(wrapper, mimetype=content_type) response['Content-Disposition'] = "attachment; filename=%s" % product_image_url return response urls.py : url(r'^download/(?P<id>\d+)/$',views.download_image, name='download_image'), settings.py STATIC_URL = '/static/' STATICFILES_DIRS=( os.path.join(BASE_DIR, 'static','static_dirs'), ) STATIC_ROOT = os.path.join(BASE_DIR, 'static','static_root') MEDIA_ROOT = os.path.join(BASE_DIR, 'static','media') MEDIA_URL = ('/media/') html : for data in images %} <a href="{% url 'download_image' data.id %}" class="btn btn-default">Download</a> {% endfor %} any idea to help me ? and if the image can be in .zip file I appreciate more. -
How to format a pandas dataframe for use with pandas-highcharts within Django
I'm trying to use pandas-highcharts within a Django project; I'm able to produce the graph, but the data is not plotted, so I'm guessing my pandas dataframe is not formatted correctly, or maybe I'm not using the right method to render the template. The result: My dataframe: Entrée d'eau - Archimède - 0013A2004166CFCD timestamp 2016-12-23 00:05:18+00:00 29.0 2016-12-23 00:05:27+00:00 29.0 2016-12-23 00:05:37+00:00 29.0 2016-12-23 00:05:47+00:00 29.0 2016-12-23 00:05:58+00:00 29.0 My view: from django.shortcuts import render from data.models import Value import pandas as pd from pandas_highcharts.core import serialize # [...] df = pd.DataFrame.from_records( Value.objects.filter(device=a).values("timestamp", "leak_value")) df.dropna(inplace=True) # not sure about this df.set_index("timestamp", inplace=True) df.sort_index(inplace=True) df = df.truncate( before=pd.to_datetime(request.POST.get("start")), after=pd.to_datetime(request.POST.get("stop"))) df = df.rename( index=str, columns={"leak_value": "{} - {} - {}".format( Room.objects.filter(unit=unit_sel).get(device=a), Device.objects.get(address=a).devicetype, a)}) print(df.head()) # DEBUG chart = serialize( df=df, render_to='Leak Values', title="Leak Values", output_type='json') return render(request, "leak_chart.html", context={"chart": chart}) My template (I include jquery and highcharts in base.html): {% extends "base.html" %} {% block body %} {% load staticfiles %} <div id="Leak Values"></div> <script type="text/javascript"> new Highcharts.Chart({{chart|safe}}); </script> {% endblock %} The page source: https://pastebin.com/EkJYQPLQ By the way I haven't found a tag for pandas-highcharts and I don't think I have the privileges to create it. I'm using pandas-highcharts 0.5.2 -
Django enforcing unique constraint on set of members in ManyToManyField
I am working in Django and I'm having some trouble enforcing a particular constraint on my data models. I have a model that consists of a group of members. class Member: name = models.CharField(max_length=100) class Team: members = models.ManyToManyField(Member) I want to enforce a constraint such that, for any given team, the set of members is unique. That is, the following teams are allowed: team_a: {A, B, C} team_b: {A, B, D} But a team_c: {C, B, A} is not allowed because the set of members is the same as team_a. How can I enforce this constraint such that an error is thrown if I try to add team_c (or any other team with the same set of members)? I have considered having a foreign key for each member and then using unique_together, but this doesn't work because I'd like the model to be unaware of ordering within the members (as there is no ordering inherent in the data). I think this might be a job for the m2m_changed signal, but I haven't been able to get it to work properly. -
DRF APIRequestFactory error
I have a view as below, class LoginAPI(APIView): def post(self, request): """ Provides login functionality. INPUT: { "username":"user_name", "password":"secret" } OUTPUT: if successful: {'success': True, 'message': 'User authenticated.'} else: {'success': False, 'message': 'Authentication failed.'} """ details = request.data try: user = authenticate(username=details['username'], password=details['password']) login(request, user) return Response({'success': True, 'message': 'User authenticated.', 'user_id': user.id}) except: return Response({'success': False, 'message': 'Authentication failed.'}, status=status.HTTP_401_UNAUTHORIZED) and I wrote simple test case as below, class APIViewsTestBase(TestCase): def setUp(self): self.factory = APIRequestFactory() self.api_client = Client() self.username = "test_user" self.password = "test_password" self.dev = Developer.objects.create(username=self.username, is_staff=True) self.dev.set_password(raw_password=self.password) self.dev.save() class TestLoginAPI(APIViewsTestBase): def test_LoginAPI(self): url = '/api/v1/login/' data = {"username": "test_user", "password": "test_password"} request = self.factory.post(path=url, data=data, format='json') view = LoginAPI.as_view() response = view(request) self.assertEqual(response.status_code, 200) When I run the test, I always getting 401 error code. Fortunately or unfortunately while I trying with Client() which is comes with django's test module, the test is passed. I executed the same instructions in django shell but, I got the same error. (not an error, response.status_code is 401)Is there any problem with my logic or code ? Somebody please help me ? -
Django-allauth google authenticaion not working in production (apache2 + mod_wsgi)
I have deployed my django web application on my institute server using apache and mod_wsgi and I am using django-allauth google authentication. My institute network uses few proxy servers to interact with the Internet. Google authentication works fine while I am running app on localhost, but as soon as I migrate the app to https://fusion.*******.ac.in, google authentication shows following Error image callback uri: https_://fusion.*******.ac.in/accounts/google/login/callback/ Please help me with this problem. -
Fixing null value in column "date_of_birth" violates not-null constraint.
I am build an Electronic Health Record Software. One of my views contians a form that captures the patients basic information. forms.py from django import forms from nesting.models import Identity_unique class Identity_Form(forms.ModelForm): NIS = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter NIS', 'class' : 'form-control' } ) ) First_Name = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter First Name', 'class' : 'form-control' } ) ) Last_Name = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter Last Name', 'class' : 'form-control' } ) ) Residence = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter Address', 'class' : 'form-control' } ) ) DOB = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter Date of Birth', 'class' : 'form-control' } ) ) class Meta: model = Identity_unique fields = ('NIS', 'First_Name', 'Last_Name', 'Residence', 'DOB',) The data shema for the modelForm models.py from django.db import models from django.contrib.auth.models import User from Identity import settings import datetime # Create your models here. class Identity_unique(models.Model): NIS = models.CharField(max_length = 200, primary_key = True) user = models.ForeignKey(settings.AUTH_USER_MODEL) Timestamp = models.DateTimeField(auto_now = True) First_Name = models.CharField(max_length = 80, null = True, ) Last_Name = models.CharField(max_length = 80, null = True, ) Residence = models.CharField(max_length = 80, blank = True ) DOB = models.DateField(auto_now = False) Unfortunately I keep getting this error … -
"JSON parse error" when sending from Ajax to Django REST Framework
I am trying to send a JSON to my restapi, builded using Django Rest Framework and all I got an error message whenever I send this request, because the other ajax call have the same structure. No matter what view I am acessing, I got the same error. I am pretty sure that the problem is on the ajax configuration or on the javascript that creates the json data, because when I send a json through 'Advanced REST client' or even through an app build using Xamarin Forms, the response is 200. function createJson( email, pass){ tmpObj = {"Email": email, "Pass": pass}; json_result = JSON.stringify(tmpObj); return json_result; } function sendLogin(){ var jsonData = createJson(State.email, State.pass); console.log(jsonData) $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: '/restapi/usuarios/login/', data: jsonData, processData: false , success: function(json) { console.log(json); State = json; first_name = getFirstName(State.Nome) title = "Login"; msg_html = "Bemvindo, <b>" + first_name + "</b>."; modal = createModal(title, msg_html); }, // handle a non-successful response error : function(xhr,errmsg,err) { var msg = "Oops! We have encountered an error: "+errmsg; console.log(msg); console.log(xhr.status + ": " + xhr.responseText); setState({sent: 'error', result: err, email: ''}); } }); } On the Server-Side: @api_view(['POST']) def UsuarioLogin(request): data = JSONParser().parse(request) … -
Vimeo 'Replace' API Endpoint Not Changing Thumbnail
I am using Vimeo's API for the users of my app to upload videos, or replace their existing video with a new one. I am using a Vimeo Client to help me make the calls in my Django Application. Uploading works without any issues, but when I try to replace an existing video with a new one, the thumbnail stays as the old video. If you play the video, it will play the new one, but the thumbnail never changes. Model Method that Uploads/Replaces def vimeo_upload(self): media_file = self.video_file if media_file and os.path.exists(media_file.path): v = vimeo.VimeoClient(token=settings.VIMEO_ACCESS_TOKEN, key=settings.VIMEO_API_KEY, secret=settings.VIMEO_API_SECRET) if self.video_url is None: try: video_uri = v.upload(media_file.path) except AssertionError as exc: logging.error('Vimeo Error: %s Video: %s' % (exc, media_file.path)) else: self.video_url = video_uri else: try: v.replace(video_uri=self.video_url, filename=media_file.path) except Exception as exc: self.video_url = None logging.error('Vimeo Replace Error: %s Video: %s' % (exc, media_file.path)) # set the video title, description, etc. if self.video_url: try: # convert locale from en-us form to en v.patch(self.video_url, data={'description': self.customer.full_name, }) except Exception as exc: logging.error('Vimeo Patch Error: %s Video: %s' % (exc, media_file.path)) Vimeo Client Model, and UploadVideoMixin class UploadVideoMixin(object): """Handle uploading a new video to the Vimeo API.""" UPLOAD_ENDPOINT = '/me/videos' REPLACE_ENDPOINT = '{video_uri}/files' def … -
How to get list elements from manytomany field in Django Rest Framework serializer
Following Django REST framework docs I can't get dict nested fields when posting from test client. My models are: class ClassificationOfDiseases(models.Model): code = models.CharField(max_length=10) description = models.CharField(max_length=300) abbreviated_description = models.CharField(max_length=190) parent = models.ForeignKey('self', null=True, related_name='children') def __str__(self): return self.abbreviated_description class Group(models.Model): experiment = models.ForeignKey(Experiment, related_name='groups') title = models.CharField(max_length=50) description = models.TextField() inclusion_criteria = \ models.ManyToManyField(ClassificationOfDiseases, blank=True) protocol_component = models.ForeignKey( ProtocolComponent, null=True, blank=True ) The serializers for that models are: class ClassificationOfDiseasesSerializer(serializers.Serializer): class Meta: model = ClassificationOfDiseases fields = ('code',) class GroupSerializer(serializers.ModelSerializer): experiment = serializers.ReadOnlyField(source='experiment.title') inclusion_criteria = ClassificationOfDiseasesSerializer(many=True, read_only=False) class Meta: model = Group fields = ('id', 'title', 'description', 'experiment', 'inclusion_criteria') def create(self, validated_data): group = Group.objects.create(experiment=validated_data['experiment'], title=validated_data['title'], description=validated_data['description']) if 'inclusion_criteria' in self.initial_data: inclusion_criteria = self.initial_data['inclusion_criteria'] print(inclusion_criteria) # DEBUG print(self.initial_data) # DEBUG for criteria in inclusion_criteria: classification_of_diseases = \ ClassificationOfDiseases.objects.filter( code=criteria['code'] ) if classification_of_diseases: group.inclusion_criteria.add( classification_of_diseases.first() ) return group And, the test posts data to the API as follows: def test_POSTing_new_group_adds_pre_existent_classification_of_diseases(self): owner = User.objects.get(username='lab1') experiment = Experiment.objects.get(nes_id=1, owner=owner) self.client.login(username=owner.username, password='nep-lab1') list_url = reverse('api_experiment_groups-list', kwargs={'experiment_nes_id': experiment.nes_id}) response = self.client.post( list_url, { 'title': 'A title', 'description': 'A description', # we post inclusion_criteria's that exists in # ClassificationOfDiseases table (this table is # pre-populated in db) 'inclusion_criteria': [ {'code': 'A00'}, {'code': 'A1782'}, {'code': 'A3681'}, … -
Django RedirectView with missing variables
Hello I want to redirect the urls like /{category}/{sub_category} to /{category}/ but RedirectView wants to resolve the url with both subcategory and category keyword arguments url(r'^/(?P<category_slug>[^\/]+)/$', CategoryView.as_view(), name='category'), url(r'^/(?P<category_slug>[^\/]+)/(?P<another_parameter>[^\/]+)$', RedirectView.as_view(pattern_name='category')), so this doesn't work, I think default should be ignoring unrequired parameters.. I am getting django.urls.exceptions.NoReverseMatch: What would be the best option to make this work? -
Cython error when pushing git on heroku master
I am getting these errors when I hit the command, "git push heroku master". It tells me that I have to install Cython. I had also installed using "pip3 install cython" but it still shows me an error. I have also changed my requirements.txt file too. -
django recurrence--get the last instance of a recurrence field
I need help on django recurrence. I am trying to get the next and last instance from a model recurrence field as shown: course = Course.objects.all() for c in course: occurrence = c.recurrences.occurrences() print(occurrence[0]) print(occurrence[-1]) This approach works but it is extremely slow. Here is the comment from the recurrence package author and suggests to use recurrences between two dates, but as I need the last instance which in some cases is infinite(end year as 9999) it's not helping with the performance. Is there any better alternative to fetch the next and last instances from recurring field? -
Custom logo in django jet
I am currently trying to do some customization to the skin of the django admin panel, to make it more in line with our brand. Currently we use django-jet to spruce up the admin panel. Are custom css/html possible with django-jet? All of the comments say that I should change some html files, but I think those files are hidden from my project because django-jet takes care of them? If anyone could point me in the right direction it would be appreciated. Thank you -
django-admin-tools 0.8 after instaling and configurating shows error 404 while trying to open admin page
I had installed and configured django-admin-tools 0.8 following the documentation. Django version is 1.11.4, using virtualenv in project. File settings.py: INSTALLED_APPS = [ 'admin_tools', 'admin_tools.theming', 'admin_tools.menu', 'admin_tools.dashboard', 'django.contrib.auth', 'django.contrib.sites', 'django.contrib.admin', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'atelierapp', ] TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], #'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.core.context_processors.request', ], 'loaders': [ 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', 'admin_tools.template_loaders.Loader', ] }, }, ] File urls.py: urlpatterns = [ #url(r'^admin/', admin.site.urls), url(r'^admin_tools/', include('admin_tools.urls')), ] But while I'm trying to enter the admin panel, there is error 404. Screenshot What is the problem I don't see? Thanks for your time. -
Django for Messaging Website, Good it Bad
Is using Django a good option to make a messaging website, coz I'm already developing other apps on Django. Will it be slower, use more processing power or anything related to that. If not Django, what framework should I use. I'm new to this framework so any kind of help would be welcomed. Thank You -
Django reverse query filter
I am trying to filter a query I have in my view. Model relation looks more or less like this: class User(Model): username = TextField() class Period(Model): date = DateField() class Bill(Model): period = ForeignKey(Period) user = ForeignKey(User) I have a list of users and I want to filter them based on whether they have a bill in certain period. I went through the docs found out that I have to make some kind of reverse lookup and came up with something like: queryset.filter(bill__period__date__month=datetime.datetime.now().month) I get the queryset from some super() call and its output is of UserQuerySet type, co I guess this one is right. However, when I check this. Also, when while debugging I make: Bill.objects.filter(period__date__month=datetime.datetime.now().month) I get a bill with a relation to user which would mean that at least one Bill with a user and thus a user from the queryset should be returned as a result. What am I missing? -
Django - databse out of synce?
When i run python manage.py syncdb i get some tables/models that are not synced like: Not synced (use migrations): -filmdb - musicdb - imagedb But when i do manage.py migrate filmdb it says that there's nothing to migrate. I think that might be the root cause for me getting the error: IntegrityError: duplicate key value violates unique constraint "database_pkey" DETAIL: Key(id)=(17523) already exists. Whenever i try to edit something through the admin panel. I also tried using migrate --fake and it didn't help. What can i do? -
Passing query string to ModelForm hidden field - Django
I want to pass a query string e.g., ?refcode='A1234' to a hidden field called inbound_referral_code in a ModelForm. My model is as follows: class User(models.Model): email = models.EmailField(max_length=255, blank=False, unique=True) inbound_referral_code = models.CharField(max_length=255) My ModelForm is currently as follows: class UserForm(forms.ModelForm): model = User fields = ['email', 'inbound_referral_code'] widgets = {'inbound_referral_code': forms.HiddenInput()} My View is: def register(request): if request.method == 'POST': form = UserForm(request.POST) [...] else: form = UserForm() return render(request, 'register.html', {'form': form}) And my template is currently: <form action="{% url 'register' %}" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit"/> </form> Two questions: How do I assign ?refcode parameter to inbound_referral_code field? What happens if ?refcode isn't provided? -
Django not showing up in requirements.txt
I have a repository that contains a web app powered by Django. Since I have done everything in a virtual environment, I figured pip freeze > requirements.txt would result in django being mentioned in requirements.txt. But no. Here is what it looks like: mongoengine==0.13.0 pymongo==3.5.1 python2==1.2 pytz==2017.2 six==1.10.0 No django. And django is installed in the right place, as you can see me prove here: >>> import django >>> django.__file__ '/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/__init__.py' The project is in this location: (workout) Sahands-MBP:workout sahandzarrinkoub$ ls README.md include pip-selfcheck.json sketches bin lib requirements.txt workout (workout) Sahands-MBP:workout sahandzarrinkoub$ pwd /Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout Why isn't django included in requirements.txt? How can I trust that pip freeze will produce the correct requirements for the project? EDIT: Another thing I noticed is that python3 isn't included in requirements.txt either, even though that is what python in my virtualenv points to. -
Understanding relations in django models fields
I get all fields of some model MyModel._meta.get_fields() and then I process them and want to understand which of them added by myself and which by django. For example if I have class Moovie and class Comedy which have moovie = models.ForeignKey(Moovie, on_delete=models.CASCADE), when i will get fields Moovie._meta.get_fields() field moovie will be in this list too, and if other models link to Moovie model, moovie field will be in list several times. I need skip them. -
Saving value of model field every month in Django
I need to save every month the value of a field of my model. I've seen Celery for scheduled tasks, but I was wondering if there is an other way to do it? My code: class Sales(models.Model): ... sales_objective = models.IntegerField(blank=True, null=True) sales_objective can change from one month to an other, and I would like then to create some stats where I can show: Sales objective for January: 123 Sales objective for February: 456 etc... -
Django - model instance being saved (or not)
I have two Django models, ModelA and ModelB. The latter has a foreign key link to the former. class ModelA(models.Model): item = models.BooleanField(default=True) class ModelB(models.Model): modela = models.ForeignKey(ModelA) answer = models.SmallIntegerField(null=True, blank=True) In the production code, an instance of ModelA is saved. During the same function, it seems to automatically save an instance of ModelB, as this is accessed in the template. def view(request): a = ModelA() a.item = True a.save() b = ModelB.objects.filter(modela_id=a.id) return render(request, 'template.html', context=locals()) Firstly, although this is how I want the code to work, I am not sure how the instance of ModelB is saved. (This is inherited code - I can't find a signal or call to ModelB anywhere else in the codebase). Secondly, this behaviour has stopped working in local development (but still works in production, with identical code); ModelB is not saved, so b returns None. I am pretty sure it's not a code issue as old branches of the code have the same problem. I have tried restoring my local db to a previous version, to no avail. The only thing that changed recently is that I squashed a load of migration files. Can anyone help with these questions? I'm running …