django-views

How to do mapping user input HTML page to view.py in django?

北慕城南 提交于 2020-05-17 09:04:09
问题 I am a newbie in Django and not able to map my user-input to my stored procedure query. views.py from django.db import connection from django.shortcuts import render from .forms import InputForm def home_view(request): print("woooooooooooo",request) context1 ={} context1['form']= InputForm() print("context1::::", context1) return render(request, "input.html", context1) def itemnumber(request): cursor = connection.cursor() try: itemnumber = "23241715" C=cursor.execute(f"EXEC

Django: How to handle error message in CreateView for UNIQUE constraint failed

泪湿孤枕 提交于 2020-05-17 08:46:54
问题 I have a generic class based createview, which generates "UNIQUE constraint failed" error. I am able to handle this and redirect it to the same createview form. However i need to send an error msg to the createview saying 'Name already exists'. How to i achieve this. model.py class Release(models.Model): name = models.CharField(max_length=200, db_index=True) class Feature(models.Model): release = models.ForeignKey(Release, on_delete=models.SET_NULL, null=True, related_name='features') name =

Can't get the detail view of a post in a social network project

二次信任 提交于 2020-05-17 07:15:06
问题 I am building a simple social network in django. In the "home" of my social, I have the list of all posts published by all users, with author and publishing date. The publishing date have a link to a url that should return the detail view of a specific post published by a user. However, as I click on it, it shows me the list of all posts published by its author (this also works when I click on the author link of the post). So both www.mysocial.com/posts/by/ciccio/7/ and www.mysocial.com/posts

Counting problem in django blog: using django ajax

夙愿已清 提交于 2020-05-17 05:58:11
问题 I created a like button for my django blog using ajax but I'm getting an error that its not counting properly, at first its 0 like in a post when i hit like it works 1 like appeared with unlike button but when i hit unlike and like again it gives 2 likes and sometimes when i unlike it show -1 like i think its jQuery problem I'm not an expert in jQuery jQuery $(document).ready(function() { function updateText(btn, newCount, verb) { btn.text(newCount + " " + verb) } $(".like-btn").click

Django - Render a List of File Names to Template

左心房为你撑大大i 提交于 2020-05-17 04:36:06
问题 I am generating a template for an image gallery page. My approach is as follows: Host the images from a sub directory of an images folder The image folder will be titled the same as the gallery title The view passes a list of filenames to the template The template loops through the list and creates img tags So my view would be def some_gallery(request): #LOGIC TO GET A LIST OF FILENAMES variables = RequestContext(request,{ 'user' : request.user, 'title' : 'something', 'files' : fileList })

Django - Render a List of File Names to Template

谁都会走 提交于 2020-05-17 04:33:29
问题 I am generating a template for an image gallery page. My approach is as follows: Host the images from a sub directory of an images folder The image folder will be titled the same as the gallery title The view passes a list of filenames to the template The template loops through the list and creates img tags So my view would be def some_gallery(request): #LOGIC TO GET A LIST OF FILENAMES variables = RequestContext(request,{ 'user' : request.user, 'title' : 'something', 'files' : fileList })

Add multiple models and custom fields to a json response in Django Rest Framework

白昼怎懂夜的黑 提交于 2020-05-15 08:09:15
问题 i'm new into Python/Django programming and i got stuck with something in a personal project that i'm doing. My issue is that i want to return a custom response based on different models of my application, some of the values will come from custom queries and others are part of the models itself. So, i have the following models in my app(some fields were deleted to not make the post too long): class Parking(models.Model): google_id = models.CharField(max_length=100) short_name = models

Django mixins for class-based-generic views

生来就可爱ヽ(ⅴ<●) 提交于 2020-05-15 02:41:25
问题 I am trying to implement staff_member_required mixins: Here are the two ways I found on how to do so: First: class StaffRequiredMixin(object): @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): if not request.user.is_staff: messages.error( request, 'You do not have the permission required to perform the ' 'requested operation.') return redirect(settings.LOGIN_URL) return super(StaffRequiredMixin, self).dispatch(request, *args, **kwargs) Second: class

Django - form.save() is not creating ModelForm

左心房为你撑大大i 提交于 2020-05-04 05:31:52
问题 In my Django application users send feedback about task. I'm creating this form with ModelForm , and after form.save() my object is not creating and is not uploading to database. Here are my codes: views.py : @login_required(login_url='sign_in') def task_details(request, slug): if slug: task = get_object_or_404(Task, slug=slug) today = datetime.now().date() deadline = task.task_deadline.date() time_left = deadline - today form = CreateFeedbackForm() if request.method == 'POST': form =

Django - form.save() is not creating ModelForm

强颜欢笑 提交于 2020-05-04 05:30:26
问题 In my Django application users send feedback about task. I'm creating this form with ModelForm , and after form.save() my object is not creating and is not uploading to database. Here are my codes: views.py : @login_required(login_url='sign_in') def task_details(request, slug): if slug: task = get_object_or_404(Task, slug=slug) today = datetime.now().date() deadline = task.task_deadline.date() time_left = deadline - today form = CreateFeedbackForm() if request.method == 'POST': form =