django-views

Rendering different part of templates according to the request values in Django

谁说胖子不能爱 提交于 2019-12-11 16:55:35
问题 In my view to render my template I receive different parameters through my request. According to these parameters I need to render different "part" in my templates. For example let say that if I receive in my request to_render = ["table", "bar_chart"] I want to render a partial template for table and an other for bar_chart to_render = ["bar_chart", "line_chart"] Then I will want to render a partial template for the bar_chart and an other for line_chart Can I define this in my view? Or do I

how to import data from .csv file to django sqlite using import.py

风格不统一 提交于 2019-12-11 16:55:32
问题 i want to retrieve data from csv file but my hardware(RFID) output gives me two different entries of same employee for timein & timeout WorkNames.csv In this there are double entries i want only one row for one employee in models.py Employee class so please help in my METHOD1 by suggesting some code which can combine 2rows into one while transfering data into sqlite OR by helping in METHOD2 by suggesting some code to import data from output.csv METHOD 1: I used import.py to import data. Data

How to render a CheckboxSelectMultiple form using forms.ModeForm that uses data from DB as :-> SomeModel.objects.filter(user=request.user)

对着背影说爱祢 提交于 2019-12-11 16:46:44
问题 Please take a look at the code and if you can not comprehend what is going on, I am explaining it in the end I have a model named Good class Good(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) ans = models.CharField(max_length=1024) and a form class GoodForm(forms.ModelForm): def __init__(self, request=None, *args, **kwargs): super(GoodForm, self).__init__(*args, **kwargs) self.input_list = request.user.love_set.all() self.fields['ans'] = forms.MultipleChoiceField(

django - insert a custom option in a select statement that is auto-generated from database table

橙三吉。 提交于 2019-12-11 16:42:21
问题 I have a drop down (select) form that pulls the select options form a database table. The options change almost all the time depending on certain variable. I need to add one more option all the way to the bottom of the drop down that will always be the same. Something that will say "more options". Any ideas? Thanks! 回答1: This is easy enough to do by overriding __init__ on the Form sub-class you're using. It should work equally well on a ModelForm as well. I'm not sure how you're populating

Unable to print the variable passed from view to template

与世无争的帅哥 提交于 2019-12-11 16:38:48
问题 I am trying to pass the variable ,I am obtaining from view to the template but it is showing in the preview of the web-browser(chrome) but not on actual screen. Following is my view file: analyzer=SentimentIntensityAnalyzer() data={} with open('today_5th_oct_new.csv','r',newline='', encoding='utf-8') as f: reader = csv.reader(f) for row in reader: data[row[0]]=float(row[1]) analyzer.lexicon.update(data) def index(request): return render(request, "gui/index.html") @csrf_exempt def output

Where to put extra logic in django web API, in the view, or in the serializer?

萝らか妹 提交于 2019-12-11 16:38:26
问题 I have a DRF view which extends CreateModelMixin and GenericViewSet. In the body of the post request, I want to accept an extra field, which does not belong to the related model. Then, I want to pop that extra field, use it for other purposes. I do not want to put that extra field back to the http response because it is a big and complex object. To explain it better here is a sample json input and the response that I want to return to the user: Request: # request { "name": "Why Nations Fail",

how to use the category name as url suffix in Django

半腔热情 提交于 2019-12-11 15:40:19
问题 Now my url is like:https://mywebsite.com/newscategory_lists-5 I want change it to:https://mywebsite.com/categoryname Here is my model: class Category(models.Model): name = models.CharField(max_length=40) # 分类名 class Meta: verbose_name = "分类" verbose_name_plural = verbose_name def __str__(self): return self.name Here is the urls.py: path('category_lists-<int:category_pk>', views.categoryNewsList, name="category_news_list"), Here is my view.py: def categoryNewsList(request, category_pk):

Dynamic nested serializers: empty validated_data

旧巷老猫 提交于 2019-12-11 15:17:15
问题 Hello everyone ! So to start here is the behavior on one of the endpoint, I am working on: # GET Request { "id": 1, "name": "test", "created_date": "date", "completed_date": "date", "template": { "name" : "test" }, => nested serializers with only the field "name" "status": 1, "results": [ { __all__ }, ... ], => nested serializers with all the fields "groups": [ { "name" }, ... ], => nested serializers with only the field "name" } # POST Request { "name": "test", "template": {"name":"test"}, =

How to get all the post in the same category in Django

我是研究僧i 提交于 2019-12-11 15:14:11
问题 I'm coding a news website.I have 'category' in News model. Now I want to get all the news in one of the categories named 'opinion'. But get: invalid literal for int() with base 10: 'opinion' here is part of my News model: class News(models.Model): category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="cate", blank=True, verbose_name='分类') here is my Category model: class Category(models.Model): name = models.CharField(max_length=40) # 分类名 class Meta: verbose_name = "分类

Django & static files while Debug mode is ON

六月ゝ 毕业季﹏ 提交于 2019-12-11 15:13:21
问题 i have some trouble with static files (images) in templates while Debug is set to True: images are not showed. here is the code of some settings and templates: http://dpaste.com/594183/ with these settings, the printed html doesn't contain the right path to static files. In any case i remember some time ago that even with the right path images are not showed, so maybe the problem is not the path. thanks, Luke 回答1: Where do you put your static files? In a new Django, you should keep them