Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
django-extensions shell_plus error - ImportError: cannot import name 'Type'
I'm using a venv in Django with Python 3.5 and Django extensions. When I try and run python manage.py shell_plus i get the following error: Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 216, in fetch_command klass = load_command_class(app_name, subcommand) File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django/core/management/__init__.py", line 36, in load_command_class module = import_module('%s.management.commands.%s' % (app_name, name)) File "/Users/lucassalmins/.pyenv/versions/3.5.1/lib/python3.5/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 673, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 662, in exec_module File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django_extensions/management/commands/shell_plus.py", line 14, in <module> from django_extensions.management.shells import import_objects File "/Users/lucassalmins/.pyenv/versions/conduit/lib/python3.5/site-packages/django_extensions/management/shells.py", line 5, in <module> from typing import ( # NOQA ImportError: cannot import name 'Type' After plenty of googling, I'm struggling to understand the issue. -
why is this not working writing ValueError (though i used Tango_with_django book 1.9)?
Am using Django 2.0.2 for Django 1.9 Tutorial and could figure out what the problem was after reconstruct my URL. Error Display (The view rango.views.category didn't return an HttpResponse object. It returned None instead.) from django.template import RequestContext from django.shortcuts import render_to_response from rango.models import Category, Page def index(request): # Obtain the context from the HTTP request. context = RequestContext(request) # Query the database for a list of ALL categories currently stored. # Order the categories by no. likes in descending order. # Retrieve the top 5 only - or all if less than 5. # Place the list in our context_dict dictionary which will be passed to the template engine. # Query for categories - add the list to our context dictionary. category_list = Category.objects.order_by('-likes')[:5] context_dict = {'categories': category_list} # looping through each category returned, and create a URL attribute. # This attribute stores an encoded URL (e.g spaces replaced with underscores). for category in category_list: category.url = category.name.replace(' ', '_') # Render the response and return to the client! return render_to_response('rango/index.html', context_dict, context) def category(request, category_name_url): # Request our context from the request passed to us. context = RequestContext(request) # Change underscores in the category name to spaces. … -
Populate Model With Initial Data
I am fairly new to Django, and am entirely at a loss on this question. I am creating a website scraper that will scrape data off of a website once a day and save it to the database. However, the website in question has kept a history of all their data. I want to populate the database with this historical data. I was thinking of scraping the historical data and saving it to a .txt file. But then I don't know what to do next? How do I populate my model with inital data set? -
Elasticsearch, save mail and search
I get mail import imaplib mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('myusername@gmail.com', 'mypassword') mail.list() # Out: list of "folders" aka labels in gmail. mail.select("inbox") # connect to inbox. How can I save Title, Date, Body, Author in Elasticsearch? I will search mail by keys. -
user login error unicode object not callable
I created MainUser model and this model has unicode method, but here I have problem like 'unicode' object is not callable this my authorization function: def auth(request): params = dict() if request.method == 'POST': try: login = request.POST['login'] password = request.POST['password'] except: messages.add_message(request, messages.WARNING, 'empty fields') return redirect(reverse('main:sing_in')) user = authenticate(username=login, password=password) if user is not None: login(request, user) return redirect('main:work') else: return redirect(reverse('main:sing_in')) return render(request, 'sing_in.html', params) and this one my mainuser model: class MainUser(AbstractBaseUser, PermissionsMixin): """ django user model """ login = models.CharField(max_length=20, blank=False, unique=True, db_index=True, verbose_name=u'Логин') first_name = models.CharField(max_length=222, blank=True, verbose_name=u'Имя') second_name = models.CharField(max_length=222, blank=True, verbose_name=u'Фамилия') email = models.EmailField(max_length=100, blank=True, verbose_name=u'email') is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) objects = MainUserManager() USERNAME_FIELD = 'login' REQUIRED_FIELDS = [] def full(self): return { "first_name": self.first_name, "second_name": self.second_name, "email": self.email } @property def is_staff(self): return self.is_admin def get_short_name(self): return self.login def get_full_name(self): return u"{0} {1}".format(self.second_name, self.first_name) def __unicode__(self): return u"{}".format(self.login) class Meta: verbose_name = u"Пользователь" verbose_name_plural = u"Пользователи" I can't understand where bug in the code, it give error when i call login(request, user) -
Store additional information in Django session in login response
After a user logs in, they're sent the session cookie inside an HttpResponse object. I want to add an additional field 'foo' to the session, just like I had done request.session['foo'] = 'bar' The above doesn't work because the login request itself doesn't have the session object itself, only subsequent requests have the cookie. How can I do this? -
(Django) How to add a queryset count for dynamic objects in a list to a template?
I am building a website with django as a place to store my university notes. I have a notes model which is linked by foreignkey to a categories model. On my homepage I use a for loop to post a link to each category page, but in brackets I also wanted to display the number of published notes in that category. For example: Biology (6) Chemistry (4) Physics (12) etc etc I use template tag to give the length eg. {{category.notes_set.all|length}} within the for loop to display the number of notes in a category, but this ignores whether the notes are published or whether they are just created. It would give a value of 7 if I had 6 published and 1 unpublished note - I want it to display 6. I really want to filter(published_date__lte=timezone.now()) but don't think this can be achieved in template. Do I have to create a context dictionary for every single category and annotate it with the count within the view? I feel this would be unmanageable when the number of categories and sub-categories becomes very large. Can I do this as a for loop within views.py? Sorry if this has an obvious answer, I … -
Django bulk create for non repeatative
I want to insert data from excel file to the databse but i want to insert only non repeatative ones i wrote this code but the if statement is always false! ef ca_import(request): uploadform=UploadFileForm(None) if request.method == 'POST' : uploadform = UploadFileForm(request.POST, request.FILES) if uploadform.is_valid(): file = uploadform.cleaned_data['docfile'] workbook = openpyxl.load_workbook(filename=file, read_only=True) # Get name of the first sheet and then open sheet by name first_sheet = workbook.get_sheet_names()[0] worksheet = workbook.get_sheet_by_name(first_sheet) data = [] try: for row in worksheet.iter_rows(row_offset=1): # Offset for header stockname =StocksName() if (StocksName.objects.filter(name=row[0].value).count()<1): stockname.name=row[0].value data.append(stockname) messages.success(request,"Successful" ,extra_tags="saveexcel") except : messages.error(request,_('Error'),extra_tags="excelerror") return render(request, 'BallbearingSite/excelfile.html',{'uploadform':uploadform}) StocksName.objects.bulk_create(data2) any suggestion to solve it ? thanks in advance -
How to display database timestamp in django, sqlite and python
I'm running Django with SQLite, and creating a log. I save the results of timezone.now() in the database, and when I retrieve it, it looks like this example: 2018-03-02 17:44:17.868267 All very well, but 1) this does not say the timezone, and it's not UTC. I happen to know it's in America/Los Angeles time. 2) I want to pretty-print the timestamp, and I'm getting lost in all the documentation that's about naive and aware and pytz and what seems like a bunch of things even further from my needs. So: presuming all the action remains in the same time zone, what functions are usable for formatting this time. For instance with day of the week, Month names, AM/PM and so on. -
How to add Group field to Custom User?
i used this Code in serializer.py group = Group.objects.get() group.user_set.add(self.object) it work fine when the Group Field have just one item and added in DB with no problem. But when i add more than one item in The Group List Field i am getting an Error: get() returned more than one Group -- it returned 2! what can i do ? -
Adding image effects in django (like mosaic)?
How can I add different effects to the image uploaded in django.? Effects like mosaic, sketch, etc... -
How to get the data from Django form and store on different variable every time user submit
views.py def post(self,request): form = HomeForm(request.POST) if form.is_valid(): I want to get the data from this fiels named post and save in different variables text = form.cleaned_data['post'] form = HomeForm() return render(request, self.template_name, {'form': form}) -
Make parallel concurrent calls to external apis from django
I have a django app (django+python+apache mod_wsgi) which act as a middleware between two systems, app gets requests from one system and it makes multiple requests to other apis to fetch and prepare the desired response, and then it passes the response to the requesting system. So it's basically acting as a one-to-many middleware, problem is making calls sequentially is taking too much time, I tried to use threading for IO concurrency. However this is not working (I've read django works on single thread; correct me if I'm wrong), I've not worked with making requests in parallel on a webserver and have no idea how to do it. Following is the current implementation : with futures.ThreadPoolExecutor(max_workers=MAX_BATCH_SIZE) as executor: future_to_url = {} for pnode in plist_node: config = {'url':rurl} futur = executor.submit(self.get_result_from_url, config) Can someone please suggest the right way to do this ? -
Any /polls URL always call index() function in views.py
polls/urls/py from django.conf.urls import url from . import views urlpatterns = [ url('', views.index, name='index'), url('<int:question_id>/', views.detail, name='detail'), # ex: /polls/5/results/ url('<int:question_id>/results/', views.results, name='results'), # ex: /polls/5/vote/ url('<int:question_id>/vote/', views.vote, name='vote'), ] views.py from __future__ import unicode_literals from django.http import HttpResponse from .models import Question from django.template import loader # from django.shortcuts import render def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] template = loader.get_template('polls/index.html') context = { 'latest_question_list': latest_question_list, } return HttpResponse(template.render(context, request)) def detail(request, question_id): return HttpResponse("You're looking at question %s." % question_id) def results(request, question_id): response = "You're looking at the results of question %s." return HttpResponse(response % question_id) def vote(request, question_id): return HttpResponse("You're voting on question %s." % question_id) url=http://127.0.0.1:8000/polls url=http://127.0.0.1:8000/polls/1/ url=http://127.0.0.1:8000/polls/1/results These all urls are giving same mapping same function index(). Any help will be appreciated -
Concat Queryset Based On Condition?
let's say i have models like follow Question and Answer class Question(models.Model): category = models.ForeignKey(Category) class Answer(models.Model): question = models.Foreignkey(Question) category = models.ForeignKey(Category) my queryset like follow questions = Question.objects.filter(category_id=1) answers = Answer.objects.filter(category_id=1) The questions queryset have all the questions for category and answers queryset have all the answers for questions per user. now I want to display all the questions with answers and warning messages to unanswered question, what is the best way to achieve this? -
Django direct assignment to the reverse side of a many-to-many set is prohibited
I am new to Python and Django. I am building APIs using Django REST framework and getting following error when I call API for MyModel. I am getting following error, after I created a ManyToMany relation Direct assignment to the reverse side of a many-to-many set is prohibited. Use label_id.set() instead. class Label(models.Model): label_id = models.CharField(db_column='labelId', primary_key=True, max_length=255) name = models.CharField(unique=True, max_length=255, blank=True, null=True) class Meta: app_label = 'my_app' managed = False db_table = 'label' class MyModel(models.Model): my_model_id = models.CharField(db_column='myModelId', primary_key=True, max_length=255) is_deactive = models.NullBooleanField(db_column='isDeactive', blank=True, null=True, default=False) labels = models.ManyToManyField(Label, through="MyModelLabel", related_name='label_id') class Meta: app_label = 'my_app' managed = False db_table = 'my_model' class MyModelLabel(models.Model): my_model_id = models.ForeignKey(MyModel, on_delete=models.CASCADE, db_column='myModelId', related_name='labels') label_id = models.ForeignKey(Label, on_delete=models.CASCADE, db_column='labelId', primary_key=True, related_name='labels') class Meta: app_label = 'my_app' managed = False db_table = 'my_model_label' unique_together = (('myModelId', 'labelId'),) serializers.py class LabelSerializer(ModelSerializer): class Meta: model = models.Label fields = '__all__' class MyModelLabelSerializer(ModelSerializer): class Meta: model = models.MyModelLabel fields = '__all__' class MyModelSerializer(ModelSerializer): labels = MyModelLabelSerializer(many=True, read_only=True) class Meta: model = models.MyModel exclude = ('is_deactive',) view.py class MyModelViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = serializers.MyModelSerializer queryset = models.MyModel.objects.all().exclude(is_deactive='1') Django Version: 2.0.2 Python Version: 3.6.3 -
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name
I want to use the default django.contrib.auth.views for resetting passwords with email confirmation. All this code is on urls.py : from django.conf.urls import url from . import views from django.contrib.auth import views as auth_views app_name = 'houses' urlpatterns = [ # Root and details page url(r'^$', views.index, name='index'), url(r'^(?P<house_id>[0-9]+)/$', views.view_house, name='index'), # Register / login / logout url(r'^register/$', views.UserFormView.as_view(), name='register'), url(r'^login/$', views.login_user, name='login_user'), url(r'^logout/$', views.logout_user, name='logout_user'), # User profiles and edit profiles url(r'^profile/$', views.view_profile, name='view_profile'), url(r'^profile/edit/$', views.edit_profile, name='edit_profile'), # ---- ERRORS ARE HERE ---- change / reset passwords url(r'^change_password/$', views.change_password, name='change_password'), url(r'^password_reset/$', auth_views.password_reset, {'post_reset_redirect': 'houses:password_reset_done',}, name='password_reset'), url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'), url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', auth_views.password_reset_confirm, name='password_reset_confirm'), ] No matter what I try i keep getting: Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name. Is it because my app name is houses? I've been trying things for hours with no luck. -
Ajax/ jquery way of checking from template if celery task is complete
I am using django + celery + RabbitMq(broker) + Redis(backend). Here's the problem: I am analysing a test, which depending on number of test takers might take a long time, hence i am using celery to do it asynchronously. Here is the view.py code: new_rl = teacher_test_analysis_new.delay(test_id,user.id) te_id = new_rl.task_id res = AsyncResult(te_id) if res.state != "PENDING": # fetch data from database and return to template new_rl = SscTeacherTestResultLoader.objects.get(test__id =test_id) return HttpResponse(new_rl) else: # return a loading gif Now the only problem is , most of the times loading gif is returned as task is still PENDING, and it gets stuck there even when task gets finished. What I want to do is ask AJAX or jquery in my template to check every few milliseconds if the task is complete and eventually load the data when it finally gets complete. How to do that? -
Django-polymorphic pre-save signal
I'm trying to switch to django-polymorphic models in my ecommerce project, but found problem with pre-save signals. When creating new product via Admin I need to fill 'Slug' field of my object with unique value. In non polymorphic version it was implemented by pre save signal from all subclasses of Parent 'Product' model: for subclass in Product.__subclasses__(): print(subclass) pre_save.connect(product_pre_save_receiver, sender=subclass) Now models hierarchy looks following (simplified): class Product(PolymorphicModel): title = models.CharField(max_length=120) class Camera(Product): serial_number = models.TextField(max_length=20, blank=True, null=True) Receiver: def product_pre_save_receiver(sender, instance, *args, **kwargs): if not instance.slug: instance.slug = unique_slug_generator(instance) What is the sender class when creating Polymorphic Child Model instance? Or is any pre_save signal even triggered in django-polymorphic? Thanks! -
Django form post object to view
I try it so many time with differect form structure. I can get the value when i use request.POST['group[name]'] But i hope i can get the value like one of this 1. request.POST['group']['name'] 2. request.POST.group.name This two form doesn't work for me. <form method="post"> <input name="group[name]" type="text" /> <input name="group[description]" type="text" /> <input name="group[image]" type="text" /> </form> <form method="post"> <input name="group.name" type="text" /> <input name="group.description" type="text" /> <input name="group.image" type="text" /> </form> Thank you -
How to filter objects based on Foreign key.
I have 2 model classes called product and product_category class product_category(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE) category_name = models.CharField(max_length=20, blank=False, null=False, default="") slug = models.SlugField(max_length=255, unique=True) class product(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE) product_name = models.CharField(max_length=50, blank=False, null=False, default="") product_description = models.TextField(max_length=280, blank=True, null=True, default="") product_type = models.ForeignKey(product_category, related_name='type_category', blank=True, null=True, on_delete=models.CASCADE) slug = models.SlugField(max_length=255, unique=True) In the views.py this is how I usually load all the products related to a single user all_category = product.objects.filter(user=user) but what I am struggling to filter is the product list based on the category they are in. As you can see, product_type is optional, so some products might not have any type. Those products should be listed at the end of the list. This is how I want to display it on the website category-1 product-1 product-2 product-3 category-2 product-4 product-5 product-6 category-3 product-7 product-8 product-9 product-10 how can I achieve this? -
The same block on several pages (in different apps) django
On the site I have a block of upcoming events. It is displayed on different pages (for each of these pages a separate view). In the template, I use {% include%} to connect "upcoming_events.html", but I don't want to use the same code in each view to get the data for "upcoming events". How best to organize the filling of the "upcoming events" block with the necessary data without duplicating the code in different places? -
Pass data from post to Serialize
I have some data {iddepart, idarrivee} to use in serializer. This data is not present in data model but used internally to compute some fields: Here is my code and I get keyError 'iddepart' class TravelViewReserveSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, mixins.DestroyModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet): serializer_class = ReservationSerializer permission_classes = (permissions.IsAuthenticated,) Model = Travel And serialiser : class ReservationSerializer(serializers.ModelSerializer): user = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CurrentUserDefault()) #user = UserSerializer() #travel = TravelSerializer() #iddepart = serializers.SerializerMethodField('iddepart') #idarrivee = serializers.SerializerMethodField('idarrivee') def create(self, validated_data): #code= random_generator() ##code = random_generator() reservation = Reservation(**validated_data) reservation.code = random_generator() reservation.save() iddepart = validated_data['iddepart'] idarrivee = validated_data['idarrivee'] -
How do you update a many to many field from a form?
I have the following two models: class TaskFile(models.Model): file = models.FileField(upload_to='task-files/') def __str__(self): return self.file.name class Task(models.Model): lesson = models.ManyToManyField(TaskFile, related_name='task_files') I have a model form to update the Task object that is already created, but the many to many relationships do not show up in the form. It just shows the option to upload a file and does not show the existing files in that object. How can I fix this? -
django-celery : TypeError: can only concatenate tuple (not "NoneType") to tuple
I have recently started using rabbitmq and celery with django. i am using django-celery, django-celery-email and post office to send emails in async way. After installing all these packages my settings.py looks like INSTALLED_APPS = [ #other apps 'djcelery', 'djcelery_email', 'post_office' ] # setup celery import djcelery djcelery.setup_loader() # using post office as the default email backend EMAIL_BACKEND = 'post_office.EmailBackend' # using djcelery's email backend as a backend for post office POST_OFFICE_BACKEND = 'djcelery_email.backends.CeleryEmailBackend' POST_OFFICE = { 'DEFAULT_PRIORITY' : 'now' } EMAIL_HOST = 'YOUR_HOST_NAME' EMAIL_HOST_USER = "YOUR_HOST_USER_NAME" EMAIL_PORT = 25 # default smtp port EMAIL_HOST_PASSWORD = "YOUR_HOST_USER_PASSWORD" EMAIL_USE_TLS = False DEFAULT_FROM_EMAIL = 'testing@example.com' when i am trying to run django-celery via command **python manage.py celeryd** It is throughing below error Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/home/ubuntu/edyodavirtual/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line utility.execute() File "/home/ubuntu/edyodavirtual/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/ubuntu/edyodavirtual/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 206, in fetch_command klass = load_command_class(app_name, subcommand) File "/home/ubuntu/edyodavirtual/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 40, in load_command_class module = import_module('%s.management.commands.%s' % (app_name, name)) File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/home/ubuntu/edyodavirtual/local/lib/python2.7/site-packages/djcelery/management/commands/celeryd.py", line 16, in <module> class Command(CeleryCommand): File "/home/ubuntu/edyodavirtual/local/lib/python2.7/site-packages/djcelery/management/commands/celeryd.py", line 20, in Command worker.get_options() + TypeError: can only concatenate tuple (not "NoneType") to tuple …