django-views

Calling a special (non-HTTP) URL from the form_valid method of a Django class-based view

☆樱花仙子☆ 提交于 2019-12-05 18:19:15
There's this HTML trick where if you do <a href="sms:14085551212?body=Hello my friend">New SMS Message</a> , clicking New SMS Message opens up the phone's native SMS app and pre-fills the To field with the number provided (1-408-555-1212 in this case), and the body with the message provided ( Hello my friend in this case). Is there any way I can call this same href string from the form_valid method of a Django class-based view? To be exact, in this form_valid method I'm receiving a POST variable that is a uuid . I need to use that uuid in the body section of the href string I've written in the

Viewset 'create' custom assign value in Django Rest Framework

你离开我真会死。 提交于 2019-12-05 18:18:19
Would like to set a CustomUser 's username by using the input email, but where to do the custom assigning, in view? At the same time it receiving a file as well. Models.py class CustomUser(AbstractUser): avatar = models.ImageField(max_length=None, upload_to='avatar', blank=True) Serializers.py class CustomUserSerializer(serializers.ModelSerializer): class Meta: model = CustomUser fields = ('id', 'first_name', 'last_name', 'email', 'password', 'avatar', 'groups') Views.py class CustomUserViewSet(viewsets.ModelViewSet): queryset = CustomUser.objects.all() serializer_class = CustomUserSerializer

How to add namespace url to a django-rest-framework router viewset

时光毁灭记忆、已成空白 提交于 2019-12-05 16:14:52
问题 I would like to add a url namespace to my api router but when I do the router still looks for urls without a namespace: router = DefaultRouter() router.register(r'users', UserViewSet) router.register(r'events', EventViewSet) router.register(r'comments', CommentViewSet) urlpatterns = patterns('apiroot.views', url(r'^', include(router.urls, namespace='api')), ) The browsable api looks for url names like 'user-list' and 'user-detail' still instead of 'api:user-list' which is what I would like to

How to cater for 'AnonymousUser' when filtering based on user

半腔热情 提交于 2019-12-05 16:02:11
I have the following model class Team(models.Model): name = models.CharField(max_length=100) members = models.ManyToManyField(User, related_name="members", blank=True, null=True) And the following view (controller) def my_teams(request): my_list = Team.objects.filter(members=request.user).order_by('name') return render_to_response('teams/index.html', {'my_list': my_list}) This works fantastic when a user is logged in, but I receive the following error while testing as an Anonymous user. Exception Value: int() argument must be a string or a number, not 'AnonymousUser' How do I cater for

Django won't serve static files while using development server

此生再无相见时 提交于 2019-12-05 14:55:45
I just started a new development server for a website I am working on and I can't seem to get the Django development server to serve the static files I have for CSS and other things. The CSS for the admin site loads fine. I am running it in a virtualenv sandbox. In settings.py I've messed around with MEDIA_ROOT and MEDIA_URL. So far for MEDIA_ROOT I've tried. MEDIA_ROOT = '/home/wluw/wluw/wluw/media' and MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media') I changed my ADMIN_MEDIA_PREFIX to ADMIN_MEDIA_PREFIX = '/admin_media/' my MEDIA_URL looks like this MEDIA_URL = '/media/' and the

Django: Generating a queryset from a GET request

元气小坏坏 提交于 2019-12-05 14:47:55
I have a Django form setup using GET method. Each value corresponds to attributes of a Django model. What would be the most elegant way to generate the query? Currently this is what I do in the view: def search_items(request): if 'search_name' in request.GET: query_attributes = {} query_attributes['color'] = request.GET.get('color', '') if not query_attributes['color']: del query_attributes['color'] query_attributes['shape'] = request.GET.get('shape', '') if not query_attributes['shape']: del query_attributes['shape'] items = Items.objects.filter(**query_attributes) But I'm pretty sure there's

Django : ModelForm with conditions

孤人 提交于 2019-12-05 14:29:42
I'm trying to create a form variable. As default Player have the level 0 and he can just change is name. Later when he is level 1, he can change is name and is avatar. When he is level 3, he can change is name, is avatar and is job. Etc... Models.py: class Player(models.Model): level = models.SmallIntegerField(default=0) name = models.CharField(max_length=50) avatar = models.URLField(default='http://default-picture.com/01.png') job = models.TextField(null=True) Fomrs.py: class ProfileForm(forms.ModelForm): class Meta: model = Player fields = ['name', 'avatar', 'job'] widgets = { 'name': forms

Class has no 'objects' member in django

不羁岁月 提交于 2019-12-05 13:40:14
from django.http import HttpResponse from .models import Destination def index(request): boards = Destination.objects.all() boards_names = list() for Destination in boards: boards_names.append(Destination.destinationtext) response_html = '<br>'.join(boards_names) return HttpResponse(response_html) I have written this code following just for practice of django framework but I am getting the following errors through pylint : E1101:Class 'Destination' has no 'objects' member E0601:Using variable 'Destination' before assignment You have two different issues, and not just one as you say: E1101

Getting all items less than a month old

蹲街弑〆低调 提交于 2019-12-05 12:48:09
问题 Is there a way to get all objects with a date less than a month ago in django. Something like: items = Item.objects.filter(less than a month old).order_by(...) 回答1: What is your definition of a "month"? 30 days? 31 days? Past that, this should do it: from datetime import datetime, timedelta last_month = datetime.today() - timedelta(days=30) items = Item.objects.filter(my_date__gte=last_month).order_by(...) Takes advantange of the gte field lookup. 回答2: items = Item.objects.filter(created_date

Django - Add rows to MySQL database

耗尽温柔 提交于 2019-12-05 11:10:53
So I already have a database setup with a few columns and a few rows already inserted in. I'm trying to create a view that you would just input information into a form and press Submit, then a row would be added to the MySQL database with the information you just typed in. I believe you can do this with admin, but I would like to try without admin and I'm not sure if this is possible? I've been using the MySQL commandline to add rows as of now.. dm03514 Of coures this is possible this is a building block for data driven websites. You can use a ModelForm as Daniel suggested (they offer built in