django-views

In django do models have a default timestamp field?

半世苍凉 提交于 2019-12-02 16:27:39
In django - is there a default timestamp field for all objects? That is, do I have to explicitly declare a 'timestamp' field for 'created on' in my Model - or is there a way to get this automagically? MoshiBin No such thing by default, but adding one is super-easy. Just use the auto_now_add parameter in the DateTimeField class: created = models.DateTimeField(auto_now_add=True) You can also use auto_now for an 'updated on' field. Check the behavior of auto_now here . For auto_now_add here . A model with both fields will look like this: class MyModel(models.Model): created_at = models

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/

最后都变了- 提交于 2019-12-02 16:26:44
问题 I was following the django documentation and making a simple poll app. I have come across the following error : Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: ^polls/ ^admin/ The current URL, , didn't match any of these." settings.py ROOT_URLCONF = 'mysite.urls' mysite/mysite/urls.py from django.conf.urls import include,url from django.contrib import admin urlpatterns = [ url(r'^polls/',include('polls.urls')), url(r'^admin/', admin.site.urls),]

NoReverseMatch django class based

女生的网名这么多〃 提交于 2019-12-02 15:12:05
问题 edit. sorry I'm having a hard time and thanks for your help how do I do that? model created_by = models.ForeignKey(User) def get_absolute_url(self): return reverse('author_update', kwargs={'pk': self.pk, 'user_id': self.created_by}) Could be related to the error Type error with django class based view I get this error NoReverseMatch at /author/add/4 Reverse for 'author_add' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['author/add/(?P<user_id>\\d+)$'] urls.py

Avoid Django def post duplicating on save

痞子三分冷 提交于 2019-12-02 15:03:23
问题 Hi I'm facing issues of duplicated objects when saving. How can I prevent that? Thanks in advance. #models.py class Candidate(models.Model): user = models.OneToOneField(User, primary_key=True) birth = models.CharField(max_length=50) ... class Job(models.Model): candidate = models.ManyToManyField('Candidate', through='CandidateToJob') title = models.CharField(max_length=500) ... class CandidateToJob(models.Model): job = models.ForeignKey(Job, related_name='applied_to') candidate = models

Django REST Framework: difference between views and viewsets?

爷,独闯天下 提交于 2019-12-02 14:14:49
May be relevant . What is the difference between views and viewsets ? And what about router and urlpatterns ? ViewSets and Routers are simple tools to speed-up implementing of your API, if you're aiming to standard behaviour and standard URLs. Using ViewSet you don't have to create separate views for getting list of objects and detail of one object. ViewSet will handle for you in consistent way both list and detail. Using Router will connect your ViewSet into "standarized" (it's not standard in any global way, just some structure that was implemented by creators of Django REST framework)

Add users to groups in Django

拟墨画扇 提交于 2019-12-02 14:01:22
问题 I am using the Group module in Django. I've created views GroupCreateView and GroupUpdateView in which I can update permissions and group name, but I also want to add users to the groups. Right now I have to update each user object and set to which groups it belongs. I want to do it the other way around where I create groups and add users to this group. How is this obtained? I guess it's something like group.user_set.add(user) 回答1: I am assuming you want to add a newly created user to an

How can i display my data in database and export it to pdf -Django

十年热恋 提交于 2019-12-02 13:03:17
my x variable is getting all the data in my database, i guess? someone help me how can i display all data and export it to pdf file. response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="WishList.pdf"' buffer = BytesIO() # Create the PDF object, using the BytesIO object as its "file." p = canvas.Canvas(buffer) x = Item.objects.all() p.drawString(100, 100,x) p.drawString(200,300,"sad") # Close the PDF object cleanly. p.showPage() p.save() # Get the value of the BytesIO buffer and write it to the response. pdf = buffer.getvalue() buffer

how to stream file to client in django

纵饮孤独 提交于 2019-12-02 12:47:17
问题 I want to know how can I stream data to client using django. The Goal The user submits a form, the form data is passed to a web service which returns a string. The string is tarballed ( tar.gz ) and the tarball is sent back to the user. I don't know what's the way. I searched and I found this, but I just have a string and I don't know if it is the thing I want, I don't know what to use in place of filename = __file__ , because I don't have file - just a string. If I create a new file for each

Django: UnboundLocalError: local variable 'company' referenced before assignment

百般思念 提交于 2019-12-02 12:29:17
问题 I am trying to make a url field in my detail view by passing two primary key in it... This is what I have done in urls.py: url(r'^company/(?P<pk1>\d+)/groupdetail/(?P<pk2>\d+)/$',views.group1DetailView.as_view(),name='groupdetail'), And in my views: def get_object(self): pk1 = self.kwargs['pk1'] pk2 = self.kwargs['pk2'] company = get_object_or_404(company, pk=pk1) group1 = get_object_or_404(group1, pk=pk2) return group1 I am getting error in this line: company = get_object_or_404(company, pk

get_success_url() takes exactly 3 arguments (2 given)

China☆狼群 提交于 2019-12-02 12:14:49
every one ,, I am using django-registration-redux (1.4) for my django registration(django 1.8),,however ,,when never I registered the web will show the error ,,but the views.py in form_valid, line 43 it is the editing function,,it seems not about the register?? views.py @login_required def edit_thing(request, slug): # grab the object... thing = ProductsTbl.objects.get(slug=slug) if thing.user != request.user: raise Http404 # set the form we're using... form_class = ProductsTblForm if request.method == 'POST': # grab the data from the submitted form form = form_class(data=request.POST,files