django-views

Standardised JSON response from views

家住魔仙堡 提交于 2020-01-17 04:26:30
问题 When my page POSTs a form to my Django view, the view returns a response with some data but soon I ran into the issue that my views returned data in different formats and different sets of information. I've thought of using JSON as a standard format to return my data. There are two types of statuses, success and failure . When the POST was successful, it just returns success but when it has failed, it returns a sub group called errors which contains a of fields and that field's error. Here's

django Url endswith regex in url path

孤街浪徒 提交于 2020-01-17 01:45:07
问题 I need to support following urls in single url regex. /hotel_lists/view/ /photo_lists/view/ /review_lists/view/ how to support all above urls in single views? I tried something like below url(r'^\_lists$/(?P<resource>.*)/$', 'admin.views.customlist_handler'), edit: hotel,photo, review is just example. that first part will be dynamic. first part can be anything. 回答1: If you wish to capture the resource type in the view, you could do this: url(r'^(?P<resource>hotel|photo|review)_lists/view/$',

Django - multiple models in one page

谁说我不能喝 提交于 2020-01-16 20:58:35
问题 I have a model that looks like this: models.py class BHA_List(models.Model): well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list') bha_number = models.CharField(max_length=100) class BHA_Drill_Bit(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_drill_bit') bit_type = models.CharField(max_length=111) class BHA_overall(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_overall') drill_str_name = models

Django - multiple models in one page

不羁的心 提交于 2020-01-16 20:57:48
问题 I have a model that looks like this: models.py class BHA_List(models.Model): well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list') bha_number = models.CharField(max_length=100) class BHA_Drill_Bit(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_drill_bit') bit_type = models.CharField(max_length=111) class BHA_overall(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_overall') drill_str_name = models

Unable to access Dictionary values in Template (object pk is the dict key)

别来无恙 提交于 2020-01-16 19:20:20
问题 I have a Django View that constructs a dictionary to a template. I have seen similar questions but no one shows how to access the dictionary value in the template using the object pk as the key (in my case the key s are pk s of the object). View code that constructs the dict: comment_uservote = {} if not current_logged_user.is_anonymous(): for comment in comments_all: try: co_vote = Vote.objects.get(user=current_logged_user, comment=comment) comment_uservote[comment.id] = co_vote.vote except

How to add Search bar for django template?

怎甘沉沦 提交于 2020-01-16 19:18:29
问题 I need a search bar in my template on top of my table. The search bar should search based on any table parameter, and filter the enteries accordingly. I implemented the search bar using CSS classes and I get it as I wanted. Now here's the views.py code. def jobs(request): jobs = Jobb.objects.all() search_term = '' if 'search' in request.GET: search_term = request.GET['search'] jobs = jobs.filter(position__icontains=search_term) context = { 'jobs': jobs, 'search_term': search_term, 'job':

How to add Search bar for django template?

痴心易碎 提交于 2020-01-16 19:18:08
问题 I need a search bar in my template on top of my table. The search bar should search based on any table parameter, and filter the enteries accordingly. I implemented the search bar using CSS classes and I get it as I wanted. Now here's the views.py code. def jobs(request): jobs = Jobb.objects.all() search_term = '' if 'search' in request.GET: search_term = request.GET['search'] jobs = jobs.filter(position__icontains=search_term) context = { 'jobs': jobs, 'search_term': search_term, 'job':

Rendering Received Webhook Payload by Django Views

喜夏-厌秋 提交于 2020-01-16 08:58:08
问题 Dev Platform: Python 3.6.2 Django 3.0 Windows 10 VScode Problem: I'm not able to render the received webhook payload on the django template via same view method or by redirect. Also I'm not able to use django sessions variable to pass payload data to another view. Seems like no session exist as per the print statements. Looks like I'm missing something cirtical. Details: settings.py My sessions middleware and installed app settings are properly set as well for cookie based sessions. INSTALLED

Query to fetch highest rated movie with mimimum 5 people rated

强颜欢笑 提交于 2020-01-16 08:35:50
问题 I want to fetch name of movie with maximum rated movie with minimum 5 people rated in django. My code : model.py class Movie(models.Model): id = models.AutoField(primary_key=True) title = models.CharField(max_length=100) vote_count = models.IntegerField() class Watchlist(models.Model): userid = models.IntegerField() movie_id = models.ForeignKey(Movie, on_delete=models.CASCADE) rating = models.IntegerField() what will be query to get movie with highest rating with minimum 5 people ? 回答1: I

In python how can I check to see if an object has a value?

走远了吗. 提交于 2020-01-16 03:29:18
问题 Base Account class BaseAccount(models.Model): user = models.ForeignKey(User, unique=True) def __unicode__(self): """ Return the unicode representation of this customer, which is the user's full name, if set, otherwise, the user's username """ fn = self.user.get_full_name() if fn: return fn return self.user.username def user_name(self): """ Returns the full name of the related user object """ return self.user.get_full_name() def email(self): """ Return the email address of the related user