Commit 7ecf0a1c authored by nikkastra's avatar nikkastra

better profile code

profile picture only, images not rendering
parent efa9a2f0
from django.contrib import admin from django.contrib import admin
# Register your models here. from .models import *
class NameAdmin(admin.ModelAdmin):
model = Name
class TasksAdmin(admin.ModelAdmin):
model = Tasks
admin.site.register(Name, NameAdmin)
admin.site.register(Tasks, TasksAdmin)
\ No newline at end of file
...@@ -2,10 +2,16 @@ from django import forms ...@@ -2,10 +2,16 @@ from django import forms
from .models import * from .models import *
class FirstName(forms.Form): class FirstName(forms.Form):
name=forms.CharField(label="Hello! What is your name? ", max_length=100) name = forms.CharField(label="Hello! What is your name? ", max_length=100)
class NicknameAndBio(forms.ModelForm): class NicknameAndBio(forms.ModelForm):
class Meta: class Meta:
model=Name model = Name
fields=['nickname', 'bio'] fields = ['nickname', 'bio']
\ No newline at end of file
class Picture(forms.ModelForm):
class Meta:
model = Name
fields = ['image']
\ No newline at end of file
# Generated by Django 3.1.7 on 2021-04-07 22:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bulletjournal', '0002_auto_20210408_0054'),
]
operations = [
migrations.AddField(
model_name='name',
name='image',
field=models.ImageField(default='static/img/default.jpg', upload_to='profilepictures'),
),
migrations.AlterField(
model_name='name',
name='bio',
field=models.CharField(default='A short bio about yourself', max_length=200),
),
migrations.AlterField(
model_name='name',
name='nickname',
field=models.CharField(default='Your nickname', max_length=50),
),
]
...@@ -2,15 +2,16 @@ from django.db import models ...@@ -2,15 +2,16 @@ from django.db import models
class Name(models.Model): class Name(models.Model):
name = models.CharField(max_length=100, unique=True) name = models.CharField(max_length=100, unique=True, default="test")
nickname = models.CharField(max_length=50, default='Your nickname') nickname = models.CharField(max_length=50, default='Your nickname')
bio = models.CharField(max_length=200, default='A short bio about yourself') bio = models.CharField(max_length=200, default='A short bio about yourself')
image = models.ImageField(upload_to='profilepictures', default='static/img/default.jpg')
def __str__(self): def __str__(self):
return '{} {} {}'.format(self.name, self.nickname, self.bio) return '{} {} {}'.format(self.name, self.nickname, self.bio)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('name_detail', args=[str(self.name)]) return reverse('name_detail', args=[str(self.pk)])
@property @property
def is_tutorial(self): def is_tutorial(self):
......
...@@ -10,6 +10,6 @@ urlpatterns = [ ...@@ -10,6 +10,6 @@ urlpatterns = [
path('key', key, name = 'key'), path('key', key, name = 'key'),
path('this_week', thisweek, name = 'thisweek'), path('this_week', thisweek, name = 'thisweek'),
path('today', today, name = 'today'), path('today', today, name = 'today'),
path('name', NameListView.as_view(), name = 'name_list'), path('names', NameListView.as_view(), name = 'name_list'),
path('name1/<int:pk>', NameDetailView.as_view(), name = 'name_detail') path('name/<int:pk>', NameDetailView.as_view(), name = 'name_detail')
] ]
\ No newline at end of file
...@@ -44,16 +44,25 @@ def home(request): ...@@ -44,16 +44,25 @@ def home(request):
def profile(request): def profile(request):
if request.method == 'POST': global name_dict
form = NicknameAndBio(request.POST)
if form.is_valid():
info = Name.objects.get(name=name_dict['name']) info = Name.objects.get(name=name_dict['name'])
info.nickname = form.cleaned_data['nickname'] picture = info.image
info.bio = form.cleaned_data['bio'] nickname = info.nickname
new_info = info.save() bio = info.bio
return redirect('name_detail', pk=new_info) if request.method == "POST":
form = Picture(request.POST, request.FILES)
if form.is_valid():
form.save()
test = Name.objects.get(name="test")
info.image = test.image
picture = info.image
info.save()
test.delete()
info_dict = {'name': name_dict['name'],'nickname': nickname, 'bio': bio, 'picture':picture, 'form':form}
return render(request, 'profile.html', info_dict)
else: else:
form = NicknameAndBio() form = Picture()
info_dict = {'name': name_dict['name'],'nickname': nickname, 'bio': bio, 'picture':picture, 'form':form}
return render(request, 'profile.html', info_dict) return render(request, 'profile.html', info_dict)
......
...@@ -124,5 +124,5 @@ USE_TZ = True ...@@ -124,5 +124,5 @@ USE_TZ = True
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'mysite/static'),] STATICFILES_DIRS = [os.path.join(BASE_DIR, 'mysite/static'),]
MEDIA_URL = '/profile pictures/' MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mysite/profile pictures/') MEDIA_ROOT = os.path.join(BASE_DIR, 'mysite/media/')
{% extends 'base.html' %} {% extends 'base.html' %}
{% block title %}{{ object.name }}{% endblock %} {% block title %}{{ Name.name }}{% endblock %}
{% block content %} {% block content %}
<h1>{{ object.name }}</h1> <h1>{{ Name.name }}</h1>
<p>kung ano man to</p> <p>kung ano man to</p>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -8,10 +8,15 @@ ...@@ -8,10 +8,15 @@
{% block content %} {% block content %}
<h1> Profile </h1> <h1> Profile </h1>
<img src="{% static 'img/picturekoparasalahat.jpg' %}" /> <img src="{{picture}}" alt="hindi ako lilitaw" />
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type='submit'>Change</button>
</form>
<ul> <ul>
<li> Franz Leonard Atanacio </li> <li> {{name}} </li>
<li> "pogi" </li> <li> {{nickname}} </li>
<li> di raw masyadong masarap pero pwede na </li> <li> {{bio}} </li>
</ul> </ul>
{% endblock %} {% endblock %}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment