django-views

Disable a method in a ViewSet, django-rest-framework

我们两清 提交于 2019-11-29 18:58:39
ViewSets have automatic methods to list, retrieve, create, update, delete, ... I would like to disable some of those, and the solution I came up with is probably not a good one, since OPTIONS still states those as allowed. Any idea on how to do this the right way? class SampleViewSet(viewsets.ModelViewSet): queryset = api_models.Sample.objects.all() serializer_class = api_serializers.SampleSerializer def list(self, request): return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) def create(self, request): return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) The definition of

How to render data to a {% included a.html %} template in Django

徘徊边缘 提交于 2019-11-29 18:07:34
I have a rank.html which is a publicly sharing template for many other templates through {% include rank.html %} method. This template will display the 48 hours hot news base on the click number. Here is the view.py: def rank(self, request): hot_news_48h = h_mostViewed(48, News, '-pv') return render(request, "rank.html", { 'hot_news_48h': hot_news_48h,}) h_mostViewed(48, News, '-pv') is a function,that can fetch most viewed(clicked) post within 48 hours.It works. Here is the rank.html: <ul> {% for hot_view in hot_news_48h %} <li> <a href="{% url 'news:news_detail' hot_view.pk %}" > <img src="{

Need to have a Required and Optional Fields in Django Formset

一世执手 提交于 2019-11-29 18:07:20
I created a formset that has maximum of 5 images to be attached; 1 - but I want the Validation to run only when the user has not attached any image (ValidationError('atleast 1 image is required')) , 2- This program is also not allowing the User to save when 1, or 2, or 3 images are attached, which I really need. So if there is 1 image or 2, that should be allowed to save. 3 - I also need to make the 1 radio-button to be selected by default, to make the selected image to be the one dispalyed in the template template <form enctype="multipart/form-data" action="" method="post"> {% csrf_token %} {

Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given

自闭症网瘾萝莉.ら 提交于 2019-11-29 18:03:40
i am trying to develop a website whith Django 2.1.3 and python 3.7.1 When i go to the homepage i get this error: TypeError at / __init__() takes 1 positional argument but 2 were given Here some details about the code i write: Trceback Environment: Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 2.1.3 Python Version: 3.7.1 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', 'crispy_forms'] Installed Middleware: ['django.middleware

Extending Django's Generic Views

≡放荡痞女 提交于 2019-11-29 17:51:50
问题 I'm writing my first app in Django, and I have a problem with the create_object Generic View; In my urls.py , I have: (r'^new$', CreateView.as_view()), The problem is that when the user submits the "new" form, I need to manipulate the data that will be saved to the database (I actually need to add the user_id foreign key); without Generic Views I used to write: form = ClientForm(request.POST) if form.is_valid(): data = form.save(commit=False) data.user = request.user data.save() form.save_m2m

Excluding fields in generic CRUD views

☆樱花仙子☆ 提交于 2019-11-29 17:05:18
I have a model named Domain which looks like this: class Domain(models.Model): """ Model for storing the company domains """ user = models.ForeignKey( User ) host = models.CharField( null=False, verbose_name="Host", max_length=128, unique=True ) I'd like to use Django's generic views for doing CRUD operations on this. There is one field in this model that needs user input but the foreign key field doesn't need any user input. How can I exclude that field from the form that my generic view generates but assign it the value of the current authenticated user. Thanks. Have a look at Russel's

How can create a model form in django with a one-to-one relation with another model

老子叫甜甜 提交于 2019-11-29 17:00:35
I want to create a model form with a one-to-one relation with another model. i.e Model1 has a one-to-one relation with Model2. I want my form to show all the fields from Model1 as well as Model2. Also what is the best way to show this in a view. You don't need to create the single form for two models. Use two django forms and place them inside the single <form> tag: class Model1Form(forms.ModelForm): class Meta: model = Model1 class Model2Form(forms.ModelForm): class Meta: model = Model2 exclude = ('model1_one_to_one_field', ) def create_models(request): if request.method == 'POST': form1 =

Django HttpResponseRedirect

大兔子大兔子 提交于 2019-11-29 17:00:14
问题 I have created a basic contact form, and when the user submits information, it should redirect to the "Thank You" page. views.py : def contact(request): # if no errors... return HttpResponseRedirect('/thanks/') urls.py : (r'^contact/$', contact), (r'^contact/thanks/$', contact_thanks), Both pages work at the hard-coded URL. However, when I submit the form on /contact/ it redirects to /contact (no ending slash), which is a nonexistent page (either a 404 or an error page telling me I need a

How to compare datetime in Django?

坚强是说给别人听的谎言 提交于 2019-11-29 15:54:57
问题 Suppose I have: ds = datetime.datetime.now dd = Entry.objects.get(pk=id).pub_date How to compare 2 objects above? I want to get the time difference between them. Please help me solve this problem. Thank you very much ! 回答1: I am assuming that pub_date is a django.db.models.DateField , which means you can treat it as a datetime.date object. If you convert them to the same type (either datetime.datetime or datetime.date ) and subtract one from the other, you will get an instance of datetime

django modelformset_factory sustains the previously submitted data even after successfully created the objects

老子叫甜甜 提交于 2019-11-29 15:46:17
问题 I am using django modelformset_factory in one of my view. I am using javascript for adding new forms to the formset in Template. Everything is working fine but my problem is that when i try to create a new object using modelformset_factory it shows me all the objects as forms which i previously created. Or in simple words it shows me the last submitted forms when i create a new instance using modelformset_factory forms. Like I have defined extra = 0 while initializing the modelformset_factory