django-views

Technique for subclassing Django UpdateCacheMiddleware and FetchFromCacheMiddleware

烈酒焚心 提交于 2019-12-07 02:27:08
问题 I've used the UpdateCacheMiddleware and FetchFromCacheMiddleware MiddleWare to enable site-wide anonymous caching to varying levels of success. The biggest problem is that the Middleware only caches an anonymous user's first request. Since a session_id cookie is set on that first response, subsequent requests by that anonymous user do not hit the cache as a result of the view level cache varying on Headers. My webpages do not meaningfully vary among anonymous users and, in so far as they do

Django: set_password isn't hashing passwords?

天涯浪子 提交于 2019-12-07 02:02:55
问题 I've made a custom User registration form/view in Django so that I can include an additional user attributes through a different model. I've used set_password to set the password of the newly created user to the password entered in the form, but I've found that the passwords that are saved aren't hashed. form: class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput()) class Meta: model = User fields = ('username', 'email', 'password') class StudentForm(forms

Django Get ImageField Path

拜拜、爱过 提交于 2019-12-07 01:34:05
问题 models image_url = models.ImageField(upload_to="uploads/shows",blank=True,null=True) I am having image_url which I need to parse it using JSON object to my android application. I only need the URL of the image(absolute/relative). I have tried image_url=myObj.file.url image_url=myObj.image_url 回答1: The correct working solution as provided by @alecxe is image_url =myObj.image_url.url 来源: https://stackoverflow.com/questions/18740651/django-get-imagefield-path

Django: Downloading uploaded files

家住魔仙堡 提交于 2019-12-07 00:09:34
I have form details in this question Django: Adding files to a form gives multiple argument error How to download the uploaded file. When i go to edit view of the form, i can see uploaded file url, but its not downloading. What setting to be changed for development and production mode? Error upon clicking link: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/media/Certificate.docx Using the URLconf defined in tiktant.urls, Django tried these URL patterns, in this order: ^ ^$ [name='home'] ^ ^login/$ [name='login'] ^ ^logout/$ [name='logout'] ^ ^logout_then_login/$

how to compress the image before uploading to s3 in django?

半城伤御伤魂 提交于 2019-12-06 21:54:29
I am working on an application where a user can upload an image. I want to reduce the size of the image in 200-500kb. This is my models.py file class Report_item(models.Model): owner = models.ForeignKey(settings.AUTH_USER_MODEL) title = models.CharField(max_length=255, help_text='*Title for the post e.g. item identity') image = models.ImageField(default="add Item image", upload_to=get_uplaod_file_name) def __str__(self): return self.title + " " + str(self.publish) And this is my views.py file class ReportCreate(generic.CreateView): model = Report_item fields = ['title','image'] def get_form

Django dynamic Form example

╄→尐↘猪︶ㄣ 提交于 2019-12-06 21:48:56
问题 I have a simple requirement for creating a dynamic form in Django - I've seen many examples but they seem to be incomplete, or require more extensive knowledge of Python and Django than I have! None show how the dynamic portion of the example should be called: This is the form class with Q1 and Q2 - I place a button on the form to add another field called Q3 - and then Q4 if pressed again: I think I got the init function semi correct: class testform(forms.Form): Q1 = forms.CharField() Q2 =

DeleteView with 2 arguements post and user

做~自己de王妃 提交于 2019-12-06 20:58:29
I have a delete view with 2 conditions "post" and "user". The user requirement is fulfilled by self.object.user = self.request.user and post requirement is fulfilled by slug = self.kwargs['slug'] (I think this may be the culprit) Are my views correct? I am new to python please forgive any silly mistakes. Views.py class ProofDelete(LoginRequiredMixin, DeleteView): model = Proof def delete(self, *args, **kwargs): return super().delete(*args, **kwargs) def get_success_url(self, *args, **kwargs): slug = self.kwargs['slug'] print(slug) obj = get_object_or_404(Post, slug=slug) url_ = obj.get

django session key changing upon authentication

点点圈 提交于 2019-12-06 20:49:29
问题 I have a Django app which records users' product choices for both authenticated users. My intention is to use the request.session.session_key variable to associate anonymous data with a user if they decide to register later, a la this post: Django storing anonymous user data However, it seems that the session key changes when the user logs in/ registers so the session key can no longer be associated with the user. Is this the correct behaviour of the Django session framework. Is there a solid

Django urls.py and views.py behaviour -

♀尐吖头ヾ 提交于 2019-12-06 19:32:31
I'm currently experimenting with Django and creating apps following the tutorials on the official website. So my urls.py looks like: urlpatterns = patterns('', (r'^/$','ulogin.views.index'), #why doesn't this work? (r'^ucode/$', 'ulogin.views.index'), (r'^ucode/(\d+)/$', 'ulogin.views.index'), ) And my views.py looks like: def index(request): return HttpResponse("Hello, world. You're at the poll index.") def redirect_to_index(request): return HttpResponseRedirect('/ucode/') When I run the server to check the test url, http://127.0.0.1:8000/ucode displays "Hello, world...etc" correctly, and

Raise 404 and continue the URL chain

匆匆过客 提交于 2019-12-06 16:36:51
问题 I've got a URLs pattern like this: urlpatterns = ( url(r'^$', list_titles, name='list'), url(r'^(?P<tag>[a-z\-0-9]+?)/$', list_titles, name='filtered-list'), url(r'^(?P<title>\S+?)/$', show_title, name='title'), ) The filtered-list and title match the same things. If there is an available list of things matching the tag in filtered-list , I want list_titles to fire off. But if there isn't a matching tag , I want to bubble that back to the URL processor so show_title fires off. If there's no