django-views

Override a form in Django admin

大兔子大兔子 提交于 2019-12-17 08:49:14
问题 In Django admin I want to override and implement my own form for a model (e.g. Invoice model). I want the invoice form to have auto-fill fields for customer name, product name and I also want to do custom validation (such as credit limit for a customer). How can I override the default form provided by Django admin and implement my own? I am new to Django, I appreciate any pointers. 回答1: You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form

django: Purpose of django.utils.functional.SimpleLazyObject?

丶灬走出姿态 提交于 2019-12-17 08:34:15
问题 I ran into an issue where I assigned request.user to a variable called prior_user , then essentially authenticated the user, then checked to see if request.user != prior_user . I expected them not to be the same and that prior_user should contain `AnonymousUser. To my surprise, they were the same. Sample code: prior_user = request.user # request object, obtained froma view authenticate_user(request) # some function that authenticates print prior_user.username != request.user.username #

django: Purpose of django.utils.functional.SimpleLazyObject?

泄露秘密 提交于 2019-12-17 08:30:35
问题 I ran into an issue where I assigned request.user to a variable called prior_user , then essentially authenticated the user, then checked to see if request.user != prior_user . I expected them not to be the same and that prior_user should contain `AnonymousUser. To my surprise, they were the same. Sample code: prior_user = request.user # request object, obtained froma view authenticate_user(request) # some function that authenticates print prior_user.username != request.user.username #

Django: CSRF token missing or incorrect

℡╲_俬逩灬. 提交于 2019-12-17 06:46:28
问题 The error is at location http://127.0.0.1:8000/fileupload/form.py I have version 1.3 of django. I have tried specifying localhost:8000 as stated in someone else's question but this did not work for me. I am trying to have a file upload form but I am receiving an error that form.py does not have the CSRF token. form.py: class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField() views.py: def upload_file(request): c = {} c.update(csrf(request)) if (not

How to handle request.GET with multiple variables for the same parameter in Django

吃可爱长大的小学妹 提交于 2019-12-17 06:31:28
问题 In a Django view you can access the request.GET['variablename'] , so in your view you can do something like this: myvar = request.GET['myvar'] The actual request.GET['myvar'] object type is: <class 'django.http.QueryDict'> Now, if you want to pass multiple variables with the same parameter name, i.e: http://example.com/blah/?myvar=123&myvar=567 You would like a python list returned for the parameter myvar , then do something like this: for var in request.GET['myvar']: print(var) However, when

Django reverse lookup of foreign keys

∥☆過路亽.° 提交于 2019-12-17 04:56:25
问题 I have a venue, this venue has many events happening there. My models look like this: class Event(models.Model): title = models.CharField(max_length=200) date_published = models.DateTimeField('published date',default=datetime.now, blank=True) date_start = models.DateTimeField('start date') date_end = models.DateTimeField('end date') def __unicode__(self): return self.title description = models.TextField() price = models.IntegerField(null=True, blank=True) venue = models.ForeignKey(Venue)

Django reverse lookup of foreign keys

こ雲淡風輕ζ 提交于 2019-12-17 04:56:22
问题 I have a venue, this venue has many events happening there. My models look like this: class Event(models.Model): title = models.CharField(max_length=200) date_published = models.DateTimeField('published date',default=datetime.now, blank=True) date_start = models.DateTimeField('start date') date_end = models.DateTimeField('end date') def __unicode__(self): return self.title description = models.TextField() price = models.IntegerField(null=True, blank=True) venue = models.ForeignKey(Venue)

Django - after login, redirect user to his custom page --> mysite.com/username

有些话、适合烂在心里 提交于 2019-12-17 04:20:14
问题 By default after login django redirects the user to an accounts/profile page or if you edit the LOGIN_REDIRECT_URL you can send the user to another page you specify in the settings.py. This is great but I would like the user (after login) to be redirected to a custom page where the link to that page would look something like this: mysite.com/username . So the default accounts/profile or the LOGIN_REDIRECT_URL settings would not work in this case since both are somehow static. In my case the

Django UserCreationForm for custom model is outputting 3 password fields password, password1, and password2

↘锁芯ラ 提交于 2019-12-14 04:22:05
问题 Ok, so I have a user registration form based on AbstractUser that replaces the Django model in my settings.py, the model only adds a couple of fields I needed for info on the users, when I tested the registration form the user did not get created, I debugged and found that both last login and date joined where required so I set them as hidden inputs with value of {% now "SHORT_DATE_FORMAT" %} that solved the first two errors however there is a third error that says the field password is

How to select only questions not yet answered by a user in Django

妖精的绣舞 提交于 2019-12-14 04:19:49
问题 I am creating a quiz and would like to show to the user only questions that were not answered by himself. How do I do that? models.py class Questao(models.Model): idQuestao = models.CharField( max_length=7, primary_key=True, null=False, verbose_name="ID da questão") class Resposta(models.Model): idQuestao = models.ForeignKey( Questao, on_delete=models.CASCADE, verbose_name="ID da questão") usuario = models.ForeignKey( User, on_delete=models.CASCADE, verbose_name="Usuário") views.py questao =