django-views

Store os.urandom variable to Sqlite database in Django

我是研究僧i 提交于 2019-12-13 06:11:56
问题 I'm trying to store random variable to Sqlite database in Django, but I get this error: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings. Here is my code: random_number = os.urandom(16) SomeModel.objects.filter(id=2).update(number=random_number) Models.py: class SomeModel(models.Model): random = models.CharField(max_length=32

Change query parameter via select

半城伤御伤魂 提交于 2019-12-13 06:01:59
问题 I have a model Dish and a model Menu: class MenuItem(models.Model): dish_name=models.TextField(unique=False) price=models.DecimalField(max_digits=5,decimal_places=2,blank=True) main_ngredient=models.TextField(unique=False) course=models.CharField(max_length=100) menu=models.ForeignKey('Menu') def __unicode__(self): return name class Menu(models.Model): restaurant=models.TextField(unique=False) year=models.IntegerField(unique=False) location=models.TextField(unique=False) status=models

Django - Attach PDF to an e-mail which was generated by a view

ぐ巨炮叔叔 提交于 2019-12-13 06:00:56
问题 I have a view which generates a PDF file and returns it as a HtmlResponse. Then the suitable URL shows that file. I want to be able to attach that pdf to an e-mail. Which is the best way to do this? Any tips or examples are appreciated! 回答1: You can use EmailMessage.attach() Assuming you have a method create_pdf that creates your PDF, you should be able to do something like this: pdf_content = create_pdf() email = EmailMessage('Hello', 'Body goes here', 'from@example.com', ['to@example.com'])

Django CreateView With ManyToManyField

青春壹個敷衍的年華 提交于 2019-12-13 05:36:06
问题 I am trying to use a Django CreateView and ultimately update a ManyToMany Field if the user selects a certain value. I can get the form to validate, but ultimately it isn't updating the manytomanyfield in the database. Not sure what I'm doing wrong. I have referenced this similar SO issue, Django ManyToMany CreateView Fields In Both Tables but it was of no help to me. I recently found this link...Django CreateView: How to perform action upon save and it seems very close to the issue I'm

Highly challenging queryset filtering, sorting and annotation (in Django-based app)

梦想的初衷 提交于 2019-12-13 05:29:09
问题 I have a Django-based web-app where users congregate and chat with one another. I've just finished writing a feature whereby users can make their own "chat groups", centered around any topic of interest. These could either be private , or publically visible. My next challenge is showing a list of all existing public groups , paginated, and sorted by the most happening group first . After some deep thinking, I've decided that the most happening group is one which sees the most unique visitors

Updating Custom User Model in Django with Class Based UpdateView

落爺英雄遲暮 提交于 2019-12-13 05:25:11
问题 I am using Django 1.7.1 with Python 3.4 . I created a custom user model and now I have a need for users to be able to update their details. What I need is that, when users go to the form to update their details, the form is pre-populated with their data i.e. username, email and so on. So far, the form is showing but not with the current user data. I have the following code: models.py from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin ... # Some code

Creating a “like” url/button for Django Rest Framework

£可爱£侵袭症+ 提交于 2019-12-13 05:21:17
问题 I'm working on an API to use for an iPhone app. In order to like a picture, I'm assuming I need to embed a url in my serializer that can be used to add a like to a photo. Could someone please help me implement this? I am able to view the url, but I'm not sure how to pass the photo's id to the view. This is where I've gotten so far: views.py: @api_view(['POST']) def like_create_api(request, photo_id): serializer = PhotoSerializer(data=request.DATA) if serializer.is_valid(): serializer.object

Passing variable to a form for display in django

拟墨画扇 提交于 2019-12-13 05:04:58
问题 views.py def when(request): user = request.user report = Report.objects.get(user=request.user) reportform = ReportForm(instance=report) settings = Settings.objects.get(user=request.user) settingsForm = SettingsForm(instance=settings) # settings=Settings.objects.get(user=2) if settings.date_format == '0': date = report.manual_date.strftime('%d/%m/%Y') else: date = report.manual_date.strftime('%m/%d/%Y') if settings.time_format == '0': time = report.manual_time.strftime('%I:%M%p') else: time =

jquery ajax loads list of results even there's nothing to query

ε祈祈猫儿з 提交于 2019-12-13 04:40:52
问题 So I am learning from a tutorial for jquery and ajax and till now I have a search form for searching and returning the search results. And it works fine. But when I delete the words from the form or backspace the words, it loads all the status in the list. What do I do to return only when there is some word and to return nothing if there's no word in the form. snippet of status.html: {% block add_account %} <input type="text" id="search" name="search" /> <ul id="search-results"> </ul> {%

Importing JSON data into Django View/Template

人走茶凉 提交于 2019-12-13 04:36:55
问题 So im working on a project and im using json data from a graphite graph and im trying to import it into the django views.py file and then get the value I want in the template. The import will be happening from a remote URL not from directly on the server itself. here is my json: [{"target": "stocks.shared (last: 4204.0)", "datapoints": [[4379.0, 1389225600], [4204.0, 1389312000]]}] This is what my views file will look like def get_context_data(self, **kwargs): context = super(IndexView, self)