django-views

Converting a function based view to a class based view with only a form and no model (object)

微笑、不失礼 提交于 2019-12-06 05:16:43
问题 Right now, this is how the password is changed within a user profile. What is the best way of converting this to a class based view knowing that there is no model involved? This is the view for changing the password @login_required def profile_change_password(request): """ Change password of user. """ user = get_object_or_404(User, username__iexact=request.user.username) if request.method == 'POST': form = PasswordChangeFormPrivate(user=user, data=request.POST) if form.is_valid(): form.save()

Can I create sub domain for each user in Django

只谈情不闲聊 提交于 2019-12-06 04:51:46
问题 I want users create their own account and users should get their own like user.foo.com and different point to different template folder. Is it possible in Django. I quite new to Django 回答1: Django itself is unaware of whatever web server you are using. Usually, to configure extra sub-domains, you need to add virtual hosts to your webserver's configuration and make sure your DNS provider forwards requests for that sub-domain to the appropriate server. To do this directly, you'll need to run

Django - Redirect with context

[亡魂溺海] 提交于 2019-12-06 04:22:20
问题 I have a Sign Up page which takes a user's username and password and saves it in the database. If the user is successfully signed up, I want to redirect him to a 'Sign In' page, with a value denoting that Signup was successful. Expected output on the redirected page: You have been successfully signed up. Sign In Username : --- Password : --- So, I am redirecting from the signup to the signin page, but I want to pass a value showing that the user has come from the Signup page and is

Use LoginRequiredMixin and UserPassesTestMixin at the same time

我的梦境 提交于 2019-12-06 03:49:29
问题 I want to have a TemplateView Class that uses LoginRequiredMixin and UserPassesTestMixin at the same time. Something like this: from django.views.generic import TemplateView from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin class FinanceOverview(LoginRequiredMixin, UserPassesTestMixin, TemplateMixin): login_url = '/login' redirect_field_name = 'next' def test_func(self): return self.request.user.groups.filter(name="FinanceGrp").exists() def get(self, request,

django - 2 views in one template

与世无争的帅哥 提交于 2019-12-06 03:18:06
问题 i have 2 different views which are getting filtered data from db. and i have to use these views in one template file(admin.html) but i cant use multiple views on a page at same time. here is my view1: draft_list = Post.objects.filter(isdraft=True).order_by("-posted") return render_to_response('userside/admin.html', {'draft_list':draft_list,}, context_instance = RequestContext(request)) view2 : publish_list = Post.objects.filter(isdraft=False).order_by("-posted") return render_to_response(

Converting a nested dictionary to a list

社会主义新天地 提交于 2019-12-06 03:07:03
问题 I know there are many dict to list questions on here but I can't quite find the information I need for my situation so I'm asking a new quetion. Some background: I'm using a hierarchical package for my models and the built-in function which generates the tree structure outputs a nested loop to indicate parents, children, etc. My goal is to keep the logic in views and output a list so that I can simply loop over it in my templates. Here is my data, in the tree structure: 1 -1.1 --1.1.1 ---1.1

Saving Hashed Version of User Password in Django Form Not Working

我们两清 提交于 2019-12-06 02:37:59
问题 I have been trying to save the hashed version of a user password but it's not working. forms.py : class up_form(forms.ModelForm): class Meta: model = Users fields =['email', 'password', 'username', 'status'] views.py : from myapp.forms import up_form from django.contrib.auth.hashers import make_password def register(request): if request.method == 'POST': sign_up = up_form(request.POST or None) if sign_up.is_valid(): sign_up.password = make_password(sign_up.cleaned_data['password']) sign_up =

Django 1.11 404 Page while Debug=True

≡放荡痞女 提交于 2019-12-06 02:31:41
Without making things difficult , I just want to show a special 404 render with staticfiles. If you set DEBUG = False you can use in urls.py handler404 = 'app.views.handler404' But it is without staticfiles. I don't want to install a web server for a simple app. With DEBUG = True in urls url(r'^404/$', views.handler400) is not overriding the default Page not found (404) page. What is the easy way to achieve a render e.g. when you type localhost/asdfhjfsda with staticfiles when DEBUG=True ? Thanks in advance... In django 1.10 docs : Changed in Django 1.9: The signature of page_not_found()

Python: Query Dict to JSON

房东的猫 提交于 2019-12-06 02:23:26
问题 I am trying to send some JSON to my django app in a query string by using encodeURIComponent() my server enpoint receives the data just fine as I can print it to the python console. print request.GET The output of the following line is in this format <QueryDict: {u'[my json array]': [u''}}> I want to convert this to JSON so I can use to get some information but I've tried using json.loads and other means of manipulating the data with no luck. My output should look like this [{u'something':

Django: How to return to previous URL

徘徊边缘 提交于 2019-12-06 02:10:14
Novice here who learned to develop a web app with python using Flask. Now I'm trying to learn django 1.9 by redoing the same app with django. Right now I am stuck at trying to get the current URL and pass it as an argument so that the user can come back once the action on the next page is completed. In Flask, to return to a previous URL, I would use the 'next' parameter and the request.url to get the current url before changing page. In the template you would find something like this: <a href="{{ url_for('.add_punchcard', id=user.id, next=request.url) }}">Buy punchcard :</a> and in the view: